【问题标题】:How toget node attributes value by its name in php simplexml?如何在 php simplexml 中通过名称获取节点属性值?
【发布时间】:2010-11-22 14:15:58
【问题描述】:
我的基本 xml 结构:
<?xml version="1.0" encoding="utf-8"?>
<users>
<user userId="1" userName="nameHere" userAge="34"></user>
</users>
然后:
如何通过名称获取特殊属性的值?就像:
variable value = user.attr('userAge')
非常感谢!!
【问题讨论】:
标签:
php
attributes
simplexml
【解决方案1】:
您可以使用attributes() 方法,
比如
$xml = simplexml_load_string('<users>
<user userId="1" userName="nameHere" userAge="34"></user>
</users>');
$userAge = (int) $xml->user->attributes()->userAge;