php xml dom getElementById and DOMXpath query fails getting element -


i have problems making function replace question , answer elements in xml file. did alot of research learning php dom i'm running stuck @ making function. problem when i'm trying parent element row attribute id returns null or when use xpath returns empty object. don't know if function work correct after the right parent element that's next step think.

my xml looks this:

<?xml version="1.0" encoding="utf-8"?> <root>   <row id="1">     <question>wat doet de vuilnisman?</question>     <answer>hij tilt de vuilnisbak in de vuilnisauto.</answer>   </row>   <row id="2">     <question>wat zegt de tandarts vaak?</question>     <answer>u mag nu spoelen.</answer>   </row> </root> 

and php function:

//modifies rows function modifyrows($file, $rowids, $rowtext) {     $xmldoc = new domdocument();     $xmldoc->preservewhitespace = false;     $xmldoc->formatoutput = true;        $xmldoc->load($file);     foreach($rowids $rowid) {         $parent = $xmldoc->getelementbyid($rowid);         for($i=0;$i<count($rowtext);$i++) {             $newq = $xmldoc->createelement('question');             $qtext = $xmldoc->createtextnode($rowtext[$i]);             $qtext = $newq->appendchild($qtext);             $newa = $xmldoc->createelement('answer');             $atext = $xmldoc->createtextnode($rowtext[$i++]);             $atext = $newa->appendchild($atext);         }         $oldq = $parent->getelementsbytagname('question');         $olda = $parent->getelementsbytagname('answer');                 $parent->replacechild($newq, $oldq);         $parent->replacechild($newa, $olda);     }        $xmldoc->save($file); } 

$rowids array contains numbers of rows modified ans rowtext in array contains question , answer strings array(0=>"question",1=>"answer") , on. xml file content can bigger or smaller adding or removing rows. have made "working" functions that.

i have solved problem thanx hakre , internet. people same problem how solved it.

    function getelementbyid($id) {         $xpath = new domxpath($this->xmldoc);         return $xpath->query("//*[@id='$id']")->item(0);     } 

Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -