从 SimpleXML 访问 @attribute [英] Accessing @attribute from SimpleXML
问题描述
我在访问 SimpleXML 对象的 @attribute
部分时遇到问题.当我 var_dump
整个对象时,我得到了正确的输出,而当我 var_dump
对象的其余部分(嵌套标签)时,我得到了正确的输出,但是当我按照文档和 var_dump
$xml->OFFICE->{'@attributes'}
,我得到一个空对象,尽管第一个 var_dump
清楚地表明有输出的属性.
I am having a problem accessing the @attribute
section of my SimpleXML object. When I var_dump
the entire object, I get the correct output, and when I var_dump
the rest of the object (the nested tags), I get the correct output, but when I follow the docs and var_dump
$xml->OFFICE->{'@attributes'}
, I get an empty object, despite the fact that the first var_dump
clearly shows that there are attributes to output.
有人知道我在这里做错了什么/我怎样才能做到这一点吗?
Anyone know what I am doing wrong here/how I can make this work?
推荐答案
您可以通过在 XML 节点上调用 attributes() 函数来获取 XML 元素的属性.然后你可以var_dump函数的返回值.
You can get the attributes of an XML element by calling the attributes() function on an XML node. You can then var_dump the return value of the function.
更多信息请访问 php.nethttp://php.net/simplexmlelement.attributes
More info at php.net http://php.net/simplexmlelement.attributes
来自该页面的示例代码:
Example code from that page:
$xml = simplexml_load_string($string);
foreach($xml->foo[0]->attributes() as $a => $b) {
echo $a,'="',$b,""
";
}
这篇关于从 SimpleXML 访问 @attribute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!