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

Get PHP 5 Power Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.