Search and replace a node in XML

Yeah.. Finally, I came back to my blog.. I was too much busy with new my job. Recently I switched to a new software company named: Codemate Bangladesh Ltd. So, I was a bit busy for last fews day that I couldn’t get the time to give any post here. Now I feel that I should share something new. Well… Recently, I had to work on a large document management application in my new office where I have worked on lots of XML documents. Then I decide I will give a post that describes some easy tricks to search in XML and replace a node. Let start the tutorial..

PHP has very nice library named SimpleXML library though it has some limitations But you can use it for adding new and search node. Basically it converts the whole xml in the object. To replace something definitely you will have to search the node first. Suppose we have XML string data like this:

$data = "<xml>
                       <name>Mr. X </name>
                       <mobile number = '01712722876' />
                      <email>[email protected]</email>
                     <name>Mr. Y </name>
                     <mobile number = '0171222876' />
                    <email>[email protected]</email>

Convert the data in SimpleXml

$xml  = new SimpleXml ($data);

Now you want to find the node: ‘mobile’ and want to add the type of number as test to it this node with keeping untouched the attribute.

$mobileNumberNodes = $xml->xpath('//mobile');

You will get the all node objects of mobile. Now iterate all nodes

     if ($mobileNumberNodes)  {

            foreach($mobileNumberNodes AS $mobileNumberNode)
               //Keep the reference of parent node
                $parentNodeOfMobileNumberNode  = $mobileNumberNode->xpath( 'parent::*' );
                $attributes = $mobileNumberNode->attributes();

               //remove the node. removeNode is a custom function that I wrote..

                $node    = $parentNodeOfMobileNumberNode[0]->addChild('mobile', 'Office');
                $node->addAttribute('number', (String) $attributes[0]);

This is a simple custom function. Actually, I have already told that SimpleXml library has some limitations. There is no function in SimpleXml library to remove a node. So, I have to import the node to Dom. Then, it’s to easy to remove the node.

     * The PHP SimpleXML library doesn't have any node remove function. So this function helps to remove the node
     * @param $node
    function removeSimpleXmlNode($node)
        $dom = dom_import_simplexml($node);

Now take a look at modified xml.

echo $xml->asXML();


      <name>Mr. X </name>
      <mobile number = '01712722876' > Office </mobile>
      <email>[email protected]</email>
  </address_boo    k>          
      <name>Mr. Y </name>
      <mobile number = '0171222876' > Office </mobile>

You can even do the whole thing by PHP Dom.


Eftakhairul Islam

Hi, I'm Eftakhairul Islam, a passionate Software Engineer, Hacker and Open Source Enthusiast. I enjoy writing about technical things, work in a couple of startup as a technical advisor and in my spare time, I contribute a lot of open source projects.


Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.

Read previous post:
Move the Close Button in Gnome-Shell to the Left from Right in Fedora 17

Recently, I installed Fedora 17 in my Desktop. Fedora 17 is cool but problem is that close button is on...