Page 1 of 1

Inserting a XML node at a specific location

Posted: Wed Jun 16, 2010 9:16 am
by leon_nerd
Hi Guys,

I want to insert a node with children at a specific location in the XML file. How do I do it?

For eg. If I have an XML like:

Code: Select all

<myvalues>
     <image name="img01">
        <src>test</src>
     </image>

     <image name="img02">
        <src>test</src>
     </image>

     <image name="img03">
        <src>test</src>
     </image>
</myvalues>
I want to insert:

Code: Select all

<image name="img11">
     <src>test</src>
  </image>
between <image name="img01"> & <image name="img02">. How do I do this? I am using SimpleXML right now to read the XML.

Thanks.

Re: Inserting a XML node at a specific location

Posted: Wed Jun 16, 2010 3:54 pm
by Sofw_Arch_Dev
SimpleXML doesn't offer much in the way of advanced XML tree manipulation, as you've undoubtedly discovered. There is no function for adding a child of a given node in a particular place, unlike Java. I could only suggest that you copy the tree node by node and insert the new node while performing the copy, athough that gets unreasonably expensive rather quickly. What's the motivation behind the specific placement; schema conformity or purely cosmetic?