Simplexml get node by attribute(Simplexml按属性获取节点)
问题描述
我有XML文件:
<?xml version="1.0" ?>
<xml>
<opis lang="en">My text</opis>
<opis lang="cz">My text2</opis>
</xml>
我要获取"My Text2"-因此属性语言为"Cz"的节点:
$xml = simplexml_load_file($fileName);
$result = $xml->xpath('//xml/opis[@lang="cz"]')
但我得到的不是值:
array(1) (
[0] => SimpleXMLElement object {
@attributes => array(1) (
[lang] => (string) cz
)
}
))
推荐答案
尝试使用DomDocument:
$xml = new DomDocument;
$xml->load('yourFile');
$xpath = new DomXpath($xml);
foreach ($xpath->query('//xml/opis[@lang="cz"]') as $rowNode) {
echo $rowNode->nodeValue; // will be 'this item'
}
这篇关于Simplexml按属性获取节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!