Inserting a XML node at a specific location

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
leon_nerd
Forum Commoner
Posts: 25
Joined: Tue Aug 21, 2007 1:20 pm

Inserting a XML node at a specific location

Post 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.
User avatar
Sofw_Arch_Dev
Forum Commoner
Posts: 60
Joined: Tue Mar 16, 2010 4:06 pm
Location: San Francisco, California, US

Re: Inserting a XML node at a specific location

Post 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?
Post Reply