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

Using XPath, SOAP, and More with Web Services

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



Creating XML from a SimpleXML Object

TheasXML()method returns a well-formed XML 1.0 string based on the SimpleXML object. Its prototype follows:

string simplexml_element->asXML()

An example follows:

$xml = simplexml_load_file("books.xml");
echo htmlspecialchars($xml->asXML());

This example returns the original XML document, except that the newline characters have been removed and the characters have been converted to their corresponding HTML entities.

Learning About a Node’s Children

Often you might be interested in only a particular node’s children. Using thechildren()method, retrieving them becomes a trivial affair. Its prototype follows:

object simplexml_element->children()

Suppose for example that thebooks.xmldocument is modified so that each book includes a cast of characters. The Hemingway book might look like the following:

     <title>The Sun Also Rises</title> 
     <author gender="male">Ernest Hemingway</author>
     <description>The masterpiece that launched Hemingway's career.</description>
<character>Jake Barnes</character>
        <character>Lady Brett Ashley</character>
        <character>Robert Cohn</character>
        <character>Mike Campbell</character>

Using thechildren()method, you can easily retrieve the characters:

$xml = simplexml_load_file("books.xml");
foreach($xml->book[2]->cast->children() AS $character) {
echo "$character<br />";

This example returns the following:

Jake Barnes
Lady Brett Ashley
Robert Cohn
Mike Campbell

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