0

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);
}
2
  • what does var_dump($dataObjects); return ? Commented Mar 6, 2016 at 9:00
  • array(1) { [0]=> object(SimpleXMLElement)#25 (4) { ["@attributes"]=> array(7) { ["a_id"]=> string(7) "4142502" ["ad_2"]=> string(7) "4503374" ["date"]=> string(10) "06.03.2016" ["id"]=> string(7) "4260040" ["std"]=> string(20) "060316234" ["status"] ... Commented Mar 6, 2016 at 9:42

1 Answer 1

1

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);
        }  
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.