class SimpleXMLElement implements Traversable, ArrayAccess, Countable, Iterator, Stringable, RecursiveIterator (View source)

Represents an element in an XML document.

Methods

__construct(string $data, int $options = 0, bool $dataIsURL = false, string $namespaceOrPrefix = "", bool $isPrefix = false)

Creates a new SimpleXMLElement object

string|bool
asXML(string $filename = null)

Return a well-formed XML string based on SimpleXML element

string|bool
saveXML(string $filename = null)

Alias of SimpleXMLElement::asXML Return a well-formed XML string based on SimpleXML element

array|false|null
xpath(string $expression)

Runs XPath query on XML data

bool
registerXPathNamespace(string $prefix, string $namespace)

Creates a prefix/ns context for the next XPath query

attributes(string $namespaceOrPrefix = null, bool $isPrefix = false)

Identifies an element's attributes

children(string $namespaceOrPrefix = null, bool $isPrefix = false)

Finds children of given node

array
getNamespaces(bool $recursive = false)

Returns namespaces used in document

array|false
getDocNamespaces(bool $recursive = false, bool $fromRoot = true)

Returns namespaces declared in document

string
getName()

Gets the name of the XML element

addChild(string $qualifiedName, string $value = null, string $namespace = null)

Adds a child element to the XML node

void
addAttribute(string $qualifiedName, string $value, string $namespace = null)

Adds an attribute to the SimpleXML element

string
__toString()

Returns the string content

int
count()

Counts the children of an element

bool
offsetExists(TKey $offset)

Class provides access to children by position, and attributes by name private Method not callable directly, stub exists for typehint only

mixed
offsetGet(TKey $offset)

Class provides access to children by position, and attributes by name private Method not callable directly, stub exists for typehint only

void
offsetSet(TKey $offset, TValue $value)

Class provides access to children by position, and attributes by name private Method not callable directly, stub exists for typehint only

void
offsetUnset(TKey $offset)

Class provides access to children by position, and attributes by name private Method not callable directly, stub exists for typehint only

void
rewind()

Rewind to the first element

bool
valid()

Check whether the current element is valid

mixed
current()

Returns the current element

mixed
key()

Return current key

void
next()

Move to next element

bool
hasChildren()

No description

getChildren()

No description

Details

__construct(string $data, int $options = 0, bool $dataIsURL = false, string $namespaceOrPrefix = "", bool $isPrefix = false)

Since: 5.0.1

Creates a new SimpleXMLElement object

Parameters

string $data

A well-formed XML string or the path or URL to an XML document if data_is_url is TRUE.

int $options

Optionally used to specify additional Libxml parameters.

bool $dataIsURL

By default, data_is_url is FALSE. Use TRUE to specify that data is a path or URL to an XML document instead of string data.

string $namespaceOrPrefix

Namespace prefix or URI.

bool $isPrefix

TRUE if ns is a prefix, FALSE if it's a URI; defaults to FALSE.

Exceptions

Exception

string|bool asXML(string $filename = null)

Since: 5.0.1

Return a well-formed XML string based on SimpleXML element

Parameters

string $filename

[optional]

If specified, the function writes the data to the file rather than returning it.

Return Value

string|bool

If the filename isn't specified, this function returns a string on success and FALSE on error. If the parameter is specified, it returns TRUE if the file was written successfully and FALSE otherwise.

string|bool saveXML(string $filename = null)

Alias of SimpleXMLElement::asXML Return a well-formed XML string based on SimpleXML element

Parameters

string $filename

[optional]

If specified, the function writes the data to the file rather than returning it.

Return Value

string|bool

If the filename isn't specified, this function returns a string on success and false on error. If the parameter is specified, it returns true if the file was written successfully and false otherwise.

array|false|null xpath(string $expression)

Runs XPath query on XML data

Parameters

string $expression

An XPath path

Return Value

array|false|null

an array of SimpleXMLElement objects or FALSE in case of an error.

bool registerXPathNamespace(string $prefix, string $namespace)

Creates a prefix/ns context for the next XPath query

Parameters

string $prefix

The namespace prefix to use in the XPath query for the namespace given in ns.

string $namespace

The namespace to use for the XPath query. This must match a namespace in use by the XML document or the XPath query using prefix will not return any results.

Return Value

bool

TRUE on success or FALSE on failure.

SimpleXMLElement|null attributes(string $namespaceOrPrefix = null, bool $isPrefix = false)

Since: 5.0.1

Identifies an element's attributes

Parameters

string $namespaceOrPrefix

[optional]

An optional namespace for the retrieved attributes

bool $isPrefix

[optional]

Default to FALSE

Return Value

SimpleXMLElement|null

a SimpleXMLElement object that can be iterated over to loop through the attributes on the tag.

Returns NULL if called on a SimpleXMLElement object that already represents an attribute and not a tag.

SimpleXMLElement|null children(string $namespaceOrPrefix = null, bool $isPrefix = false)

Since: 5.0.1

Finds children of given node

Parameters

string $namespaceOrPrefix

[optional]

An XML namespace.

bool $isPrefix

[optional]

If is_prefix is TRUE, ns will be regarded as a prefix. If FALSE, ns will be regarded as a namespace URL.

Return Value

SimpleXMLElement|null

a SimpleXMLElement element, whether the node has children or not.

array getNamespaces(bool $recursive = false)

Since: 5.1.2

Returns namespaces used in document

Parameters

bool $recursive

[optional]

If specified, returns all namespaces used in parent and child nodes. Otherwise, returns only namespaces used in root node.

Return Value

array

The getNamespaces method returns an array of namespace names with their associated URIs.

array|false getDocNamespaces(bool $recursive = false, bool $fromRoot = true)

Since: 5.1.2

Returns namespaces declared in document

Parameters

bool $recursive

[optional]

If specified, returns all namespaces declared in parent and child nodes. Otherwise, returns only namespaces declared in root node.

bool $fromRoot

[optional]

Allows you to recursively check namespaces under a child node instead of from the root of the XML doc.

Return Value

array|false

The getDocNamespaces method returns an array of namespace names with their associated URIs.

string getName()

Since: 5.1.3

Gets the name of the XML element

Return Value

string

The getName method returns as a string the name of the XML tag referenced by the SimpleXMLElement object.

SimpleXMLElement|null addChild(string $qualifiedName, string $value = null, string $namespace = null)

Since: 5.1.3

Adds a child element to the XML node

Parameters

string $qualifiedName

The name of the child element to add.

string $value

[optional]

If specified, the value of the child element.

string $namespace

[optional]

If specified, the namespace to which the child element belongs.

Return Value

SimpleXMLElement|null

The addChild method returns a SimpleXMLElement object representing the child added to the XML node.

void addAttribute(string $qualifiedName, string $value, string $namespace = null)

Since: 5.1.3

Adds an attribute to the SimpleXML element

Parameters

string $qualifiedName

The name of the attribute to add.

string $value

The value of the attribute.

string $namespace

[optional]

If specified, the namespace to which the attribute belongs.

Return Value

void

No value is returned.

string __toString()

Since: 5.3

Returns the string content

Return Value

string

Returns string representation of the object that implements this interface (and/or "__toString" magic method).

int count()

Counts the children of an element

Return Value

int

The custom count as an integer.

The return value is cast to an integer.

bool offsetExists(TKey $offset)

Class provides access to children by position, and attributes by name private Method not callable directly, stub exists for typehint only

Parameters

TKey $offset

An offset to check for.

Return Value

bool

true on success or false on failure.

The return value will be casted to boolean if non-boolean was returned.

mixed offsetGet(TKey $offset)

Class provides access to children by position, and attributes by name private Method not callable directly, stub exists for typehint only

Parameters

TKey $offset

The offset to retrieve.

Return Value

mixed

Can return all value types.

void offsetSet(TKey $offset, TValue $value)

Class provides access to children by position, and attributes by name private Method not callable directly, stub exists for typehint only

Parameters

TKey $offset

The offset to assign the value to.

TValue $value

The value to set.

Return Value

void

void offsetUnset(TKey $offset)

Class provides access to children by position, and attributes by name private Method not callable directly, stub exists for typehint only

Parameters

TKey $offset

The offset to unset.

Return Value

void

void rewind()

Rewind to the first element

Return Value

void

Any returned value is ignored.

bool valid()

Check whether the current element is valid

Return Value

bool

The return value will be casted to boolean and then evaluated. Returns true on success or false on failure.

mixed current()

Returns the current element

Return Value

mixed

Can return any type.

mixed key()

Return current key

Return Value

mixed

TKey on success, or null on failure.

void next()

Move to next element

Return Value

void

Any returned value is ignored.

bool hasChildren()

Since: 8.0

No description

Return Value

bool

true if the current entry can be iterated over, otherwise returns false.

RecursiveIterator|null getChildren()

Since: 8.0

No description

Return Value

RecursiveIterator|null

An iterator for the current entry.