php - Simplexml get node by attribute -
i've got xml file:
<?xml version="1.0" ?> <xml> <opis lang="en">my text</opis> <opis lang="cz">my text2</opis> </xml>
i want "my text2" - node attribute lang "cz":
$xml = simplexml_load_file($filename); $result = $xml->xpath('//xml/opis[@lang="cz"]')
but instead of value get:
array(1) ( [0] => simplexmlelement object { @attributes => array(1) ( [lang] => (string) cz ) } ))
try using domdocument:
$xml = new domdocument; $xml->load('yourfile'); $xpath = new domxpath($xml); foreach ($xpath->query('//xml/opis[@lang="cz"]') $rownode) { echo $rownode->nodevalue; // 'this item' }
Comments
Post a Comment