PHP Function Reference

PHP - SimpleXML



The SimpleXML extension provides a very simple and easily usable tool-set that allows us to easily manipulate and get XML data. It provides an easy way of getting an element's name, attributes and textual content if you know the XML document's structure or layout. SimpleXML turns an XML document into a data structure that can be iterated through like a collection of arrays and objects.

Installation

There is no installation needed to use this extension. This extension is a part of the PHP core. This extension is enabled by default. It may be disabled by using the following option at compile time: --disable-simplexml.

Runtime Configuration

This extension has no configuration directives defined in php.ini.

The SimpleXMLElement class

Class Description
SimpleXMLElement The SimpleXMLElement class.
MethodsDescription
__construct() Creates a new SimpleXMLElement object.
addAttribute() Adds an attribute to the SimpleXML element.
addChild() Adds a child element to the XML node.
asXML() Returns a well-formed XML string based on SimpleXML element.
attributes() Identifies an element's attributes.
children() Finds children of given node.
count() Counts the children of an element.
getDocNamespaces() Returns namespaces declared in document.
getName() Gets the name of the XML element.
getNamespaces() Returns namespaces used in document.
registerXPathNamespace() Creates a prefix/ns context for the next XPath query.
saveXML() Returns a well-formed XML string based on SimpleXML element. Alias of asXML() function.
toString() Returns the string content.
xpath() Runs XPath query on XML data.

The SimpleXMLIterator class

Class Description
SimpleXMLIterator The SimpleXMLIterator class.
MethodsDescription
current() Returns the current element.
getChildren() Returns the sub-elements of the current element.
hasChildren() Checks whether the current element has sub elements.
key() Returns current key.
next() Move to next element.
rewind() Rewind to the first element.
valid() Check whether the current element is valid.

The SimpleXML Functions

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

PHP SimpleXML Predefined Constants

This extension has no constants defined.