从SimpleXML访问@attribute [英] Accessing @attribute from SimpleXML
问题描述
访问我的SimpleXML对象的@attribute
部分时遇到问题.当我var_dump
整个对象时,我得到正确的输出,而当我var_dump
其余对象(嵌套标签)时,我得到正确的输出,但是当我遵循docs和var_dump
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节点上调用attribute()函数来获取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.net http://php.net/simplexmlelement.attributes
More info at php.net http://php.net/simplexmlelement.attributes
该页面上的示例代码:
$xml = simplexml_load_string($string);
foreach($xml->foo[0]->attributes() as $a => $b) {
echo $a,'="',$b,"\"\n";
}
这篇关于从SimpleXML访问@attribute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!