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

Going deeper into parsing XML strings: comparing nodes - 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.

  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



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:

<?xml version="1.0" encoding="iso-8859-1"?>
<name>John Doe</name>
<address>Binary Avenue 1234 FL</address>
<gender type="male">1</gender>
<gender type="female">2</gender>
<name>Janet Smith</name>
<address>Crazy Bits Road 4568 CA</address>
<gender type="male">1</gender>
<gender type="female">2</gender>
<name>James Smith</name>
<address>Socket Boulevard 7894 OH</address>
<gender type="male">1</gender>
<gender type="female">2</gender>
<name>Silvia Wilson</name>
<address>Protocol Avenue 5652 NY</address>
<gender type="male">1</gender>
<gender type="female">2</gender>
<name>Alejandro Gervasio</name>
<address>Boulevard of Objects 10101 AR</address>
<gender type="male">1</gender>
<gender type="female">2</gender>

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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