Home arrow PHP arrow Page 3 - Web Services: SimpleXML

Loading XML - PHP

In this third part of a five-part series on Web Services, we'll wrap up our discussion of MagpieRSS and move on to SimpleXML, an intuitive methodology for processing XML structures. 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. Web Services: SimpleXML
  2. SimpleXML
  3. Loading XML
  4. Parsing the XML
By: Apress Publishing
Rating: starstarstarstarstar / 1
August 05, 2010

print this article



A number of SimpleXML functions are available for loading and parsing the XML document. These functions are introduced in this section, along with several accompanying examples.

Note  To take advantage of SimpleXML when using PHP versions older than 6.0, you need to disable the PHP directivezend.ze1_compatibility_mode.  

Loading XML from a File

Thesimplexml_load_file()function loads an XML file into an object. Its prototype follows:

object simplexml_load_file(string filename [, string class_name])

If a problem is encountered loading the file,FALSEis returned. If the optionalclass_nameparameter is included, an object of that class will be returned. Of course,class_nameshould extend theSimpleXMLElementclass. Consider an example:

$xml = simplexml_load_file("books.xml");

This code returns the following:

object(SimpleXMLElement)#1 (1) {
  array(3) {
object(SimpleXMLElement)#2 (3) {
      string(19) "Pride and Prejudice"
      string(11) "Jane Austen"
      string(32) "Jane Austen's most popular work."
    object(SimpleXMLElement)#3 (3) {
      string(14) "The Conformist"
      string(15) "Alberto Moravia"
      string(46) "Alberto Moravia's classic psychological novel."
    object(SimpleXMLElement)#4 (3) {
      string(18) "The Sun Also Rises"
      string(16) "Ernest Hemingway"
      string(55) "The masterpiece that launched Hemingway's

Note that dumping the XML will not cause the attributes to show. To view attributes, you need to use theattributes()method, introduced later in this section.

Loading XML from a String

If the XML document is stored in a variable, you can use thesimplexml_load_string()function to read it into the object. Its prototype follows:

object simplexml_load_string(string data)

This function is identical in purpose tosimplexml_load_file(), except that the lone input parameter is expected in the form of a string rather than a file name.

Loading XML from a DOM

The Document Object Model (DOM) is a W3C specification that offers a standardized API for creating an XML document, and subsequently navigating, adding, modifying, and deleting its elements. PHP provides an extension capable of managing XML documents using this standard, titled the DOM XML extension. You can use thesimplexml_import_dom()function to convert a node of a DOM document into a SimpleXML node, subsequently exploiting use of the SimpleXML functions to manipulate that node. Its prototype follows:

object simplexml_import_dom(domNode node)

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