php_ref_simplexml
PHP SimpleXML Functions Reference
Section titled “PHP SimpleXML Functions Reference”PHP SimpleXML Introduction
Section titled “PHP SimpleXML Introduction”SimpleXML is a PHP extension providing a very simple and easily usable toolset to convert XML to an object that can be processed with normal property selectors and array iterators.
It’s particularly useful for reading and extracting data from XML documents with a known structure. It represents elements, attributes, and text content as accessible object properties and array elements.
While powerful for common tasks, SimpleXML might have limitations with complex namespaces, very large files (memory usage), or when needing fine-grained control over XML parsing (where DOMDocument or XMLReader might be more suitable).
Installation
Section titled “Installation”The SimpleXML extension is part of the PHP core and is enabled by default. No separate installation is required.
Key SimpleXML Functions and Methods
Section titled “Key SimpleXML Functions and Methods”These are primary functions for loading XML and methods available on SimpleXMLElement objects:
| Function/Method | Description |
|---|---|
simplexml_load_file() | Interprets an XML file into a SimpleXMLElement object. Returns false on failure. |
simplexml_load_string() | Interprets a string of XML into a SimpleXMLElement object. Returns false on failure. |
simplexml_import_dom() | Get a SimpleXMLElement object from a DOM node (created via DOMDocument). |
__construct() | (Method) Creates a new SimpleXMLElement object, typically called implicitly by simplexml_load_*. |
asXML() / saveXML() | (Method) Returns a well-formed XML string (version 1.0) based on the SimpleXMLElement object. Can optionally save to a file. |
xpath() | (Method) Runs an XPath query on the XML data and returns an array of SimpleXMLElement objects or false. |
registerXPathNamespace() | (Method) Creates a namespace context for the next XPath query. |
addAttribute() | (Method) Adds an attribute to the SimpleXML element. Requires namespace parameter if applicable. |
addChild() | (Method) Adds a child element to the SimpleXML element. Requires namespace parameter if applicable. |
getName() | (Method) Gets the name of the XML element. |
getNamespaces() | (Method) Returns the namespaces used in the document (relevant for the current node). |
getDocNamespaces() | (Method) Returns the namespaces declared in the document’s root node. |
attributes() | (Method) Identifies an element’s attributes. Returns a SimpleXMLElement object containing attributes, which can be iterated or accessed by name. |
children() | (Method) Finds the children of a given node. Can optionally specify a namespace. |
count() | (Method) Counts the children of an element. |
SimpleXML Iteration (SimpleXMLElement implements Traversable)
Section titled “SimpleXML Iteration (SimpleXMLElement implements Traversable)”SimpleXMLElement objects can often be iterated directly using foreach to access child elements of the same name. The internal Iterator interface methods can also be conceptually understood:
| Iterator Concept / Method | Description |
|---|---|
current() | Returns the current element in the iteration. |
key() | Returns the key (often the tag name or index) of the current element. |
next() | Moves the iterator to the next element. |
rewind() | Resets the iterator to the first element. |
valid() | Checks if the current iterator position is valid (i.e., points to an element). |
You typically don’t call these iterator methods directly, but rely on foreach to handle the iteration process.
For more details and examples, refer to the official PHP Manual: