XML DOMDocument PHP - Get node where attribute value -


purpose :

create method retrieve nested element specific "id".

what have tried :

  • retrieve code x path expressions via domxpath class query method. every time empty node list returned
    • '/row[@id=' . $id . ']'
    • '//table/row[@id=' . $id . ']'
    • '//[@id=' . $id . ']'
  • tried domdocument::getelementbyid , set domdocument::$validateonparse right after instantiating domdocument class. returns empty node list.

example xml:

<?xml version="1.0" encoding="utf-8"?> <table>   <row id="1">     <job>construction</job>     <age>34</age>     <name>bob</name>   </row>   <row id="2">     <job>construction</job>     <age>34</age>     <name>bob</name>   </row>   <row id="3">     <job>construction</job>     <age>34</age>     <name>bob</name>   </row>   <row id="4">     <job>construction</job>     <age>34</age>     <name>bob</name>   </row> </table> 

php code :

class simpleorm{      ...      public function find($id)     {         settype($id, "int");         $xpath = new domxpath($this->_dom);         $expression = '/row[@id=' . $id . ']';         return $xpath->query($expression);     }  } 

the full php code can read here -> https://github.com/danoon/simpleorm/blob/master/simpleorm.php

question :

why proper element/node not returned , how can achieve this?

calling find function

$users = new simpleorm("users"); $result = $users->find(1); 

try this

    <?php     $slideids = array();     $get_id = 2;      $xml = new domdocument();     $xml->load('test.xml'); // path of xml file ,make sure path correct     $xpd = new domxpath($xml);     false&&$result_data = new domelement(); //this ide have intellysense     $result = $xpd->query("//row[@id=".$get_id."]/*");  // change table naem here     foreach($result $result_data){         $key = $result_data->nodename;         $values = $result_data->nodevalue;         $slideids[$key] = $values;     }      echo '<pre/>';     print_r($slideids); ?> 

sort of reference manual xpath queries/expressions


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -