Mohammad
Mohammad

Reputation: 327

PHP use $xml->xpath multiple

I want use $xml->xpath for an XML file.

The first line below works successfully but second line does not work.

$xml = simplexml_load_string(decode(file_get_contents('http://www.file.net/name.xml')));
if($xml) {
    $dataObjects = $xml->xpath('/feed/in/test[@id="0603162"]'); // First line
    $xmlObjects = $dataObjects->xpath('/get/type[@name="333"]'); // Second line
    print_r($xmlObjects);
}

Upvotes: 0

Views: 51

Answers (1)

splash58
splash58

Reputation: 26143

You made too many errors as in Xpath syntax so in logic. A code may be about such: 1) play the second xpath to each elements but not for a list 2) use dot at begginig of the second xpath to find into an element but not in the full document

if($xml) {
    $dataObjects = $xml->xpath('/feed/in/test[@id="0603162]'); // First line
    foreach($dataObjects as $xmlObject) {
        $Objs = $xmlObject->xpath('./get/type[@name="333]'); // Second line
        foreach($Objs as $Obj) 
          print_r($Obj);
        }  
}

Upvotes: 1

Related Questions