O'Reilly logo

XML Parsing with PHP by John M. Stokes

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

Advanced SimpleXML

Most of SimpleXML’s advanced features relate to namespaces, discussed in Chapter 1

Namespaces in SimpleXML

Parsing with Namespaces

Let’s load the Flex document in Appendix E and create a SimpleXML object with it. Recall from Chapter 2 that we can do this two ways: by loading the data first and then passing it to SimpleXML or by loading directly with simplexml_load_file().

//Load the file into a variable first
$FlexDoc = file_get_contents('FlexDoc.xml');
$mxml = new SimpleXMLElement($FlexDoc);

//OR
//Load the file directly
$mxml = simplexml_load_file('FlexDoc.xml');

Most of SimpleXML’s functions accept a $namespace variable, including the constructor, children() and attribute() functions. Setting $namespace and $is_prefix ...

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