Connecting Tech Pros Worldwide Forums | Help | Site Map

Adding Attributes to all XML nodes

Familiar Sight
 
Join Date: Apr 2006
Posts: 133
#1: Jan 18 '08
Howdy,

I have an PHP page that edits XML files. I want ADD a new *id* attribute to all nodes on the page that do not have it all ready. Then i want to delete all of the values of *id* and set them as an incremental 1-x values down the page.

This is the current code i am using to edit specific nodes.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $content = $_POST['content'];
  3. $id = $_POST['id'];
  4. $node = $_POST['node'];
  5. $explode = explode("_",$node);
  6.  
  7. $dom=new DOMDocument();
  8. $dom->load('sample.xml');
  9. $dom->formatOutput = true;
  10. //echo $dom->saveXML(); // show before file
  11. $allnodes = $dom->getElementsByTagName($explode[0]);
  12. foreach ($allnodes as $nodes) {
  13.     if ($nodes->nodeName==$explode[0] and $nodes->getAttribute('id')==$id) { //
  14.         $nodes->setAttribute($explode[1],$content);
  15.     }
  16. }
  17. $dom->save('sample.xml');
  18. ?>
  19.  
This is the XML file im working with. NOTE the missing *id* node of the 2nd *child01* node.
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <parent option01="other info" option02="other info" id="1">
  3.     <child01 option01="child01 option01" option02="child01 option02" id="2">
  4.         <child02 option01="child02 option01" option02="child02 option02" id="3">
  5.             <text option01="text option01" option02="text option02" id="4">
  6.                 <![CDATA[<b>Ma quande lingues coalesce</b>]]>
  7.             </text>
  8.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02" id="5">image 1</image>
  9.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02" id="6">image 2</image>
  10.         </child02>
  11.         <child02 option01="child02 option01" option02="child02 option02" id="7">
  12.             <text option01="text option01" option02="text option02" id="8">
  13.                 <![CDATA[Lorem ipsum dolor sit amet.]]>
  14.             </text>
  15.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02" id="9">image 1</image>
  16.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02" id="10">image 2</image>
  17.         </child02>
  18.     </child01>
  19.     <child01 option01="child01 option01" option02="child01 option02">
  20.         <child02 option01="child02 option01" option02="child02 option02">
  21.             <text option01="text option01" option02="text option02">
  22.                 <![CDATA[Epsum factorial non deposit quid pro quo hic escorol.]]>
  23.             </text>
  24.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02">image 1</image>
  25.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02">image 2</image>
  26.         </child02>
  27.         <child02 option01="child02 option01" option02="child02 option02">
  28.             <text option01="text option01" option02="text option02">
  29.                 <![CDATA[Li Europan lingues es membres del sam familie.]]>
  30.             </text>
  31.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02">image 1</image>
  32.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02">image 2</image>
  33.         </child02>
  34.     </child01>
  35. </parent>
  36.  

Reply