访问我的SimpleXML对象的@attribute
部分时遇到问题。当我var_dump
整个对象时,我得到了正确的输出,当我var_dump
对象的其余部分(嵌套标记)时,我得到了正确的输出,但是当我遵循文档和var_dump
$xml->OFFICE->{'@attributes'}
时,我得到了一个空对象,尽管第一个var_dump
清楚地表明有要输出的属性。
有人知道我在这里做错了什么吗/我如何才能做到这一点?
发布于 2009-10-31 04:37:35
您可以通过调用XML节点上的attributes()函数来获取XML元素的属性。然后,您可以var_dump函数的返回值。
有关更多信息,请访问php.net http://php.net/simplexmlelement.attributes
来自该页面的示例代码:
$xml = simplexml_load_string($string);
foreach($xml->foo[0]->attributes() as $a => $b) {
echo $a,'="',$b,"\"\n";
}
发布于 2012-09-26 18:57:48
尝尝这个
$xml->attributes()->Token
发布于 2013-09-04 22:45:39
我以前使用过很多次来获取@attributes
,就像下面这样,而且它有点长。
$att = $xml->attributes();
echo $att['field'];
它应该更简单,并且你可以一次只获取以下格式的属性:
标准方式-数组-访问属性(AAA)
$xml['field'];
其他替代方案包括:
右键快速格式化(&Q)
$xml->attributes()->{'field'};
格式错误
$xml->attributes()->field;
$xml->{"@attributes"}->field;
$xml->attributes('field');
$xml->attributes()['field'];
$xml->attributes->['field'];
https://stackoverflow.com/questions/1652128
复制相似问题