我有这个表格:
<item><parameter name="a">3</parameter></item>
是否可以使用 SimpleXML 读取“a”?
我已经试过了 $xml->item->parameter->getName();但它只返回“参数”。
提前致谢。
最佳答案
是的,使用 SimpleXML
中的 attributes()
方法
echo (string)$xml->item->parameter->attributes()->name;
替代方案是使用 xpath()
$name = $xml->xpath('item/parameter/@name');
echo $name[0];
xpath()
总是返回数组(如果出错则返回 false),这就是为什么你需要将它分配给一个 var,或者如果你有 php >= 5.4 你可以使用 array dereferencing
echo $xml->xpath('item/parameter/@name')[0];
关于php - 使用 SimpleXML 读取参数名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11341426/