Home arrow PHP arrow Page 2 - Using XPath, SOAP, and More with Web Services

Using XPath to Retrieve Node Information - PHP

In this fourth part of a five-part series on Web Services, you'll learn about SimpleXML objects, SOAP, and more. This article is excerpted from chapter 20 of the book Beginning PHP and Oracle: From Novice to Professional, written by W. Jason Gilmore and Bob Bryla (Apress; ISBN: 1590597702).

  1. Using XPath, SOAP, and More with Web Services
  2. Using XPath to Retrieve Node Information
  3. SOAP
  4. PHP’s SOAP Extension
By: Apress Publishing
Rating: starstarstarstarstar / 2
August 09, 2010

print this article



XPath is a W3C standard that offers an intuitive, path-based syntax for identifying XML nodes. For example, referring to thebooks.xmldocument, you could use thexpath()method to retrieve allauthornodes using the expression/library/book/author:

array simplexml_element->xpath(string path)

XPath also offers a set of functions for selectively retrieving nodes based on value.

Suppose you want to retrieve all authors found in thebooks.xmldocument:

$xml = simplexml_load_file("books.xml");
$authors = $xml->xpath("/library/book/author");
foreach($authors AS $author) {
echo "$author<br />";

This example returns the following:

Jane Austen
Alberto Moravia
Ernest Hemingway

You can also use XPath functions to selectively retrieve a node and its children based on a particular value. For example, suppose you want to retrieve allbooktitles where the author is namedErnest Hemingway:

$xml = simplexml_load_file("books.xml");
$book = $xml->xpath("/library/book[author='Ernest Hemingway']");
echo $book[0]->title;

This example returns the following:

The Sun Also Rises

>>> More PHP Articles          >>> More By Apress Publishing

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: