PHP Simple XML
The SimpleXML extension provides a very simple and easily usable toolset to convert XML to an object that can be processed with normal property selectors and array iterators.
For example, there are given an 'example.php' file
<?php
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies>
<movie>
<title>PHP: Behind the Parser</title>
<characters>
<character>
<name>Ms. Coder</name>
<actor>Onlivia Actora</actor>
</character>
<character>
<name>Mr. Coder</name>
<actor>El ActÓr</actor>
</character>
</characters>
<plot>
So, this language. It's like, a programming language. Or is it a
scripting language? All is revealed in this thrilling horror spoof
of a documentary.
</plot>
<great-lines>
<line>PHP solves all my web problems</line>
</great-lines>
<rating type="thumbs">7</rating>
<rating type="stars">5</rating>
</movie>
</movies>
XML;
?>
Example to getting plot element’s data from 'example.php'
<?php
include 'example.php';
$movies = new SimpleXMLElement($xmlstr);
echo $movies->movie[0]->plot;
?>
Output
So, this language. It's like, a programming language. Or is it a
scripting language? All is revealed in this thrilling horror spoof
of a documentary.
Requirements
This extension requires the libxml PHP extension.
This means that passing in --enable-libxml is also required, although this is implicitly accomplished because libxml is enabled by default.
Installation
This extension is enabled by default.
It may be disabled by using the following option at compile time: --disable-simplexml
Run-time Configuration
This extension has no configuration directives defined in php.ini.
Resource Types
This extension has no resource types defined.
Predefined Constants
This extension has no constants defined.
SimpleXML Functions
-
simplexml_import_dom — Get a SimpleXMLElement object from a DOM node
-
simplexml_load_file — Interprets an XML file into an object
-
simplexml_load_string — Interprets a string of XML into an object
SimpleXML Predefined Classes
SimpleXMLElement — The SimpleXMLElement class
-
SimpleXMLElement::addAttribute — Adds an attribute to the SimpleXML element
-
SimpleXMLElement::addChild — Adds a child element to the XML node
-
SimpleXMLElement::asXML — Return a well-formed XML string based on SimpleXML element
-
SimpleXMLElement::attributes — Identifies an element's attributes
-
SimpleXMLElement::children — Finds children of given node
-
SimpleXMLElement::__construct — Creates a new SimpleXMLElement object
-
SimpleXMLElement::count — Counts the children of an element
-
SimpleXMLElement::getDocNamespaces — Returns namespaces declared in document
-
SimpleXMLElement::getName — Gets the name of the XML element
-
SimpleXMLElement::getNamespaces — Returns namespaces used in document
-
SimpleXMLElement::registerXPathNamespace — Creates a prefix/ns context for the next XPath query
-
SimpleXMLElement::saveXML — Alias of SimpleXMLElement::asXML
-
SimpleXMLElement::__toString — Returns the string content
-
SimpleXMLElement::xpath — Runs XPath query on XML data
SimpleXMLIterator — The SimpleXMLIterator class
-
SimpleXMLIterator::current — Returns the current element
-
SimpleXMLIterator::getChildren — Returns the sub-elements of the current element
-
SimpleXMLIterator::hasChildren — Checks whether the current element has sub elements
-
SimpleXMLIterator::key — Return current key
-
SimpleXMLIterator::next — Move to next element
-
SimpleXMLIterator::rewind — Rewind to the first element
-
SimpleXMLIterator::valid — Check whether the current element is valid