php - 访问 xml 对象

标签 php xml api

我正在使用 YouTube API。我从 API 得到以下结果:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [rel] => alternate
            [href] => http://www.youtube.com/watch?v=blabla
        )

)

我对这个对象感到困惑。我想访问 @attributes。我该怎么做?

最佳答案

print_r 输出的 @attributes 部分只是可以通过 $obj['attrname'] 访问的元素的属性。

<?php
$obj = new SimpleXMLElement('<foo rel="alternate" href="http://www.youtube.com/watch?v=blabla" />');
print_r($obj); // to verify that the sample data fits your actual data

echo $obj['rel'], ' | ', $obj['href'];

prints

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [rel] => alternate
            [href] => http://www.youtube.com/watch?v=blabla
        )

)
alternate | http://www.youtube.com/watch?v=blabla

另见 Example #5 Using attributes在 SimpleXML 文档中。

关于php - 访问 xml 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35055603/

相关文章:

php - 文字装饰 : none; not working

c# - 将 XML 反序列化为 C# XmlRoot 中的对象不起作用

java - PreferenceScreen 中的 ColorAccent 在 API <=22 中无法正常工作

python - Django Rest Framework 在序列化程序中获取用户

javascript - JS 问题通过在 2 个不同的 div 框中显示获取的 MySQL 数据 [PHP, MySQL & JS]

php - 从字符串中提取美国电话号码

php - Symfony 配置默认值

Java 正则表达式到 xml

api - 如何禁用phonegap自动重载(至少在开发期间)

api - 访问共享日历(例如通过 Office 365 REST API 访问 session 室)