Find and replace nodes in XML

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
thelambinatore
Forum Newbie
Posts: 1
Joined: Fri May 28, 2010 4:33 am

Find and replace nodes in XML

Post by thelambinatore »

Hi, new here and pretty new to php.

Basically what i'm looking to do is search an XML file for any node of a particular type, and replace it. I am fine locating all the nodes that need replacing but the problem i'm having is actually replacing them. I've done many searches but can't seem to find anything exactly relevant to what i want to do. I'll give an example to better illustrate what i'm trying to do..

i search for all nodes in an xml document, for example:
<nodeone>content</nodeone>

then i create a new node from a string, for example:
$dom = DOMDocument::loadXML("<newnode><nodechild></nodechild></newnode>");

i'm not sure how much of a difference this makes but it's worth noting here that in my case the new node(s) are generated dynamically so I wont know the content or structure of them, also the new node could contain any number of child nodes, but I can ensure it is valid XML.

So my question is how would i then replace the original node (</nodeone>) with, essentially, the contents of of $dom? Can this be achieved?

Any help would be much appreciated, i am a bit stuck on this one! Thanks!
Post Reply