O'Reilly logo

PHP 5 Power Programming by Derick Rethans, Stig Sæther Bakken, Andi Gutmans

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

8.4. SimpleXML

The SimpleXML extension, enabled by default in PHP 5, is the easiest way to work with XML. You don't need to remember a difficult DOM API. You just access the XML through a data structure representation. Here are its four simple rules:

  1. Properties denote element iterators.

  2. Numeric indices denote elements.

  3. Non-numeric indices denote attributes.

  4. String conversion allows access to TEXT data.

By using these four rules, you can access all the data from an XML file.

8.4.1. Creating a SimpleXML Object

You can create a SimpleXML object in any of three ways, as shown in this example:

 <?php $sx1 = simplexml_load_file('example.xml'); $string = <<<XML <?xml version='1.0'?> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required