PHP
  Home arrow PHP arrow Page 2 - 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 - Going deeper into parsing XML strings: comparing nodes
    ( Page 2 of 5 )



    To demonstrate how nodes of a given XML string can be compared, first I’ll create a PHP file that will contain the string in question. Here’s the source code for this file:

    $xmlstr=<<<XML
    <?xml version="1.0" encoding="iso-8859-1"?>
    <users>
    <user>
    <name>John Doe</name>
    <address>Binary Avenue 1234 FL</address>
    <email>john@john-domain.com</email>
    <gender type="male">1</gender>
    <gender type="female">2</gender>
    </user>
    <user>
    <name>Janet Smith</name>
    <address>Crazy Bits Road 4568 CA</address>
    <email>janet@janet-domain.com</email>
    <gender type="male">1</gender>
    <gender type="female">2</gender>
    </user>
    <user>
    <name>James Smith</name>
    <address>Socket Boulevard 7894 OH</address>
    <email>james@james-domain.com</email>
    <gender type="male">1</gender>
    <gender type="female">2</gender>
    </user>
    <user>
    <name>Silvia Wilson</name>
    <address>Protocol Avenue 5652 NY</address>
    <email>silvia@silvia-domain.com</email>
    <gender type="male">1</gender>
    <gender type="female">2</gender>
    </user>
    <user>
    <name>Alejandro Gervasio</name>
    <address>Boulevard of Objects 10101 AR</address>
    <email>alejandro@alejandro-domain.com</email>
    <gender type="male">1</gender>
    <gender type="female">2</gender>
    </user>
    </users>
    XML;

    All right, now that I have an XML string to work with, take a look at the example below, which first loads the XML data onto an object and next compares a specific node with a predefined value:

    require_once 'xml_string.php';
    if(!$xml=simplexml_load_string($xmlstr)){
        trigger_error('Error reading XML string',E_USER_ERROR);
    }
    $message=(string)$xml->user[4]->name=='Alejandro Gervasio'?'User
    found!':'User not found!';
    echo $message;

    With this example, you can learn how XML nodes can be compared and evaluated appropriately. Notice that prior to performing the comparison, string type casting is applied to the selected node, because nodes are accessed originally as objects.

    With reference to the previous example, its output is the following:

    User found!

    Since the example you saw before is really simple, here’s another one, which also illustrates how to compare a different node of the same XML string:

    require_once 'xml_string1.php';
    if(!$xml=simplexml_load_string($xmlstr)){
        trigger_error('Error reading XML string',E_USER_ERROR);
    }
    $message=(string)$xml->user[4]->name=='Unexistent user'?'User not
    found!':'User found!';
    echo $message;

    In this case, after executing the above script, this is the result that I get on my browser:

    User not found!

    As you can see, comparing nodes that belong to a given XML string is a straightforward process that doesn’t bear much discussion here. Therefore, let’s move on and learn how to locate specific XML nodes using another interesting function included with the “simpleXML” extension. I’m talking about the “Xpath()” method, which will be explained in the next few lines. Please keep on reading.



     
     
    >>> 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 4 Hosted by Hostway
    Stay green...Green IT