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'
}
相关文章