fork download
  1. <?php
  2.  
  3. $xmlstr = <<<EOF
  4. <root>
  5.   <!-- ... -->
  6.   <B ID="444">
  7.   <C></C>
  8.   <D>Anything</D>
  9.   <E>
  10.   <child1>foo</child1>
  11.   <child2>data2</child2>
  12.   <child3>data3</child3>
  13.   <child4>data4</child4>
  14.   </E>
  15.   </B>
  16.   <!-- ... -->
  17. </root>
  18. EOF;
  19.  
  20. $xml = new SimpleXMLElement($xmlstr);
  21. $query = "//B[@ID = 444 and .//child2 = 'data2' and .//child4 = 'data4']";
  22. $result = $xml->xpath($query);
  23. $dirty = False;
  24.  
  25. foreach ($result as $b) {
  26. $child2 = $b->xpath(".//child2")[0];
  27. $child4 = $b->xpath(".//child4")[0];
  28.  
  29. echo (string)$child2;
  30. echo (string)$child4;
  31.  
  32.  
  33.  
  34. $child2[0] = "Blabla";
  35. $child4[0] = "Blaaaaaaaa";
  36.  
  37. $dirty = True;
  38. }
  39.  
  40. if ($dirty) {
  41. // $xml->saveXML($File);
  42. }
  43.  
  44. echo $xml->saveXML();
  45. ?>
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
data2data4<?xml version="1.0"?>
<root>
   <!-- ... -->
   <B ID="444">
        <C/>  
        <D>Anything</D>
        <E>
            <child1>foo</child1>
            <child2>Blabla</child2>
            <child3>data3</child3>
            <child4>Blaaaaaaaa</child4>
        </E>
    </B>
   <!-- ... -->
</root>