PHP
  Home arrow PHP arrow Page 3 - 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 - Finding nodes inside a XML string: using the “Xpath()” method
    ( Page 3 of 5 )

    With regard to the localization of specific nodes within an XML string, the “simpleXML” extension comes with the handy “Xpath()” method, which allows you to locate particular nodes and iterate over them by utilizing a “foreach” loop. The following example demonstrates how to achieve this, so take a look at its source code:

    // search <name> nodes
    require_once 'xml_string.php';
    if(!$xml=simplexml_load_string($xmlstr)){
        trigger_error('Error reading XML string',E_USER_ERROR);
    }
    foreach($xml->xpath('//name') as $names){
        echo 'Name node found with a value of '.$names.'<br />';
    }

    The script listed above uses the “Xpath()” method to find all the <name> nodes contained within the previous sample XML string. Notice the remarkable flexibility provided by this method, when searching nodes that match a given name. Simple and powerful, right?

    And, of course, here is the corresponding output of the prior code snippet:

    Name node found with a value of John Doe
    Name node found with a value of Janet Smith
    Name node found with a value of James Smith
    Name node found with a value of Silvia Wilson
    Name node found with a value of Alejandro Gervasio

    In case the above example isn’t clear enough to you, here's an additional one, which searches for and displays all the <email> nodes included within the respective sample XML data string:

    // search <email> nodes
    require_once 'xml_string.php';
    if(!$xml=simplexml_load_string($xmlstr)){
        trigger_error('Error reading XML string',E_USER_ERROR);
    }
    foreach($xml->xpath('//email') as $email){
        echo 'Email node found with a value of '.$email.'<br />';
    }

    As you may have guessed, all the <email> nodes are properly located and displayed as follows:

    Email node found with a value of john@john-domain.com
    Email node found with a value of janet@janet-domain.com
    Email node found with a value of james@james-domain.com
    Email node found with a value of silvia@silvia-domain.com
    Email node found with a value of alejandro@alejandro-domain.com

    At this stage, hopefully you have learned how to search specific nodes within a given XML string by using the handy “Xpath()” method. Now, it’s time to see how these nodes can be replaced appropriately. Therefore, go ahead and read the next section.



     
     
    >>> 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