XML : Updating specific xml node with php dom

I'm trying to make php code that updates only required nodes from xml via xpath.

Here's example of php code:

      // get variables      $toimipaikka = $_GET["toimipaikka"];       $ohje = $_GET["ohje"];      $uusohje = $_GET["uusohje"];        //load file      $ohjeet = new DomDocument;      $ohjeet->load('genesis.xml');      $xpolku = new DomXpath($ohjeet);        // find specific nodes and update   foreach ($xpolku->query('//ohjeet/ohje[@toimipaikka="' . $toimipaikka . '"]/osa[@ohj="' . $ohje . '"]') as $node) {    $node->parentNode->replaceChild($node, $uusohje);  }        // save xml file        file_put_contents('genesis.xml',$ohjeet->saveXML());    

And here is example of xml file:

  <ohjeet>       <ohje toimipaikka="kaikki">           <osa ohj="ohj_1101"><![CDATA[          <a href="apple">link link link<a>          ]]></osa>      </ohje>      <ohje toimipaikka="labra">           <osa ohj="ohj_110"><![CDATA[          <li>text text text</li>          ]]></osa>          <osa ohj="ohj_110"><![CDATA[          <li>text text text</li>          ]]></osa>      </ohje>  </ohjeet>    

It opens the file and saves it but no changes are made. Any advises? I'm pretty sure the problem is in the foreach loop but just cant find it.

No comments:

Post a Comment