Home arrow PHP arrow Page 3 - Searching and Replacing Nodes with SimpleXML in PHP 5

Finding nodes inside a XML string: using the “Xpath()” method - PHP

Want to learn how to get the most out of the “simpleXML” extension that comes bundled with PHP 5? Welcome to the last part of the series “Working with simpleXML in PHP 5.” In three tutorials, this series covers topics ranging from the basics of parsing XML files with this library, to performing advanced tasks, such as searching, extracting and replacing nodes, and interoperating with the XML DOM.

TABLE OF CONTENTS:
  1. Searching and Replacing Nodes with SimpleXML in PHP 5
  2. Going deeper into parsing XML strings: comparing nodes
  3. Finding nodes inside a XML string: using the “Xpath()” method
  4. Replacing nodes within a XML string: using the “asXML()” method
  5. Using a few additional methods: finding child nodes, accessing attributes and using the XML DOM
By: Alejandro Gervasio
Rating: starstarstarstarstar / 11
June 26, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: