PHP
  Home arrow PHP arrow Page 4 - Searching and Replacing Nodes with SimpleXML in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Searching and Replacing Nodes with SimpleXML in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 9
    2006-06-26


    Table of Contents:
  • Searching and Replacing Nodes with SimpleXML in PHP 5
  • Going deeper into parsing XML strings: comparing nodes
  • Finding nodes inside a XML string: using the “Xpath()” method
  • Replacing nodes within a XML string: using the “asXML()” method
  • Using a few additional methods: finding child nodes, accessing attributes and using the XML DOM

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Searching and Replacing Nodes with SimpleXML in PHP 5 - Replacing nodes within a XML string: using the “asXML()” method
    ( Page 4 of 5 )

    Fortunately, the “simpleXML” extension isn’t limited to finding specific nodes within a given XML string or file. You can replace particular XML elements by using the cool “asXML()” method with ease. But first of all, let me show you how this method can be utilized for replacing nodes on the fly.

    Using the same XML string that you saw in the previous sections, here is how this method is used for substituting a particular element:

    // setting new values for specific nodes with the asXML() method
    require_once 'xml_string.php';
    if(!$xml=simplexml_load_string($xmlstr)){
        trigger_error('Error reading XML string',E_USER_ERROR);
    }
    $xml->user[0]->name='Jack Norton';
    echo $xml->asXML();

    If you examine the above script, you’ll possibly find it quite interesting. Please notice how the first user’s name is replaced with a different one by using the “asXML()” method. In this case, the logic of the script is very simple: first, the XML string is loaded onto an object, then the node being replaced is located by using the corresponding array syntax, and finally the “asXML()” method is invoked.

    In fact, this method returns a new XML string, where the value of the selected node has been replaced with the new one, as shown below:

    Jack Norton
    Binary Avenue 1234 FL
    John@domain.com 1 2 Janet Smith
    Crazy Bits Road 4568 CA
    Janet@janet-domain.com 1 2 James Smith
    Socket Boulevard 7894 OH
    James@james-domain.com 1 2 Silvia Wilson
    Protocol Avenue 5652 NY
    Silvia@silvia-domain.com 1 2 Alejandro Gervasio
    Boulevard of Objects 10101 AR
    Alejandro@alejandro-domain.com 1 2

    As you’ll certainly agree, the behavior of this method is very convenient, since it only generates a new XML string on the fly, and leaves the original XML data untouched. Based on this example, try replacing other nodes and watch the different results that you’ll get in each case.

    All right, now you know how to search and replace the nodes of a XML string, which is good and simple. So, what’s next? In the next section, I’ll be showing you how to use some additional methods of the “simpleXML” extension, in order to locate child nodes, access attributes and work with the XML DOM.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT