Index

XMLElement

MMO\core\helpers · 1 symbol · generated 5 Oct 2026

class

XMLElement

XMLElement.php:16

SimpleXMLElement wrapper, adding: namespace-prefixed attribute/child access ('ns:name'), parent/sibling navigation, a self-contained toXML serializer, and various convenience mutators SimpleXMLElement itself lacks (setAttribute, addElement, removeAttribute, ...).

class MMO\core\helpers\XMLElement extends SimpleXMLElement
Namespace
MMO\core\helpers
Extends
SimpleXMLElement
You need to call static method FromString or FromFile to load correctly the xml.
$xml = XMLElement::FromString('<root a="1"><child>text</child></root>'); $xml->getAttribute('a'); // '1' $xml->getChild('child')->getInnerText(); // 'text'

Index

addElement($tag, $attributes, $children)Add child element (prefix supported)
addNamespace($prefix, $namespace)Declare a namespace on this element (xmlns:$prefix="$namespace"), without leaving a dummy attribute behind (a standard SimpleXML add-then-unset trick).
addXML($xml, $namespace)Add xml to this node
clear()Clear node
FromFile($filename, $options, $namespace_or_prefix, $is_prefix) static
FromString($xml, $options, $namespace_or_prefix, $is_prefix) static
getAllChildren()
getAttribute($name)Get attribute and prefixed attribute's value as a string.
getAttributes($namespace, $isPrefix)
getBoolAttribute($name)Get boolean attribute (prefix supported).
getChild($tag)
getInnerText()Get inner text (ignore all children)
getName($withPrefix)Override so you can get the name with the prefix
getNextSibling()
getParentNode()
getPreviousSibling()
remove()Remove node from xml
removeAttribute($name)Remove attribute (prefix supported)
reorderNodes($fn)Reorder nodes using a function
reorderNodesByTag($tagOrder)Reorders child nodes according to a given tag order list.
setAttribute($name, $value)Set attribute's value (prefix supported).
setAttributes($attributes)Set multiple attributes (prefix supported)
setInnerText($text)Set inner text and replace all children by the text
toXML($declaredNs, $indent)Export node to xml

Methods24

addElement($tag, $attributes, $children)

XMLElement.php:247

Add child element (prefix supported)

function addElement( string $tag, array $attributes = [], SimpleXMLElement|string ...$children ): static
$tagstring
$attributes = []array
...$childrenSimpleXMLElement | string
returnsstatic
<foo:a>Apple</foo:a> $this->addElement('foo:a');

addNamespace($prefix, $namespace)

XMLElement.php:80

Declare a namespace on this element (xmlns:$prefix="$namespace"), without leaving a dummy attribute behind (a standard SimpleXML add-then-unset trick). NOTE: due to a SimpleXML quirk, getNamespaces may not immediately reflect the new declaration without re-parsing the XML -- asXML / toXML output does include it right away.

function addNamespace(string $prefix, string $namespace): self
$prefixstringthe prefix name
$namespacestringthe namespace uri
returnsself$this, for chaining
$node->addNamespace('foo', 'http://example.com/foo');

FromFile($filename, $options, $namespace_or_prefix, $is_prefix)

staticXMLElement.php:36
static function FromFile( string $filename, int $options = 0, string $namespace_or_prefix = '', bool $is_prefix = false ): static
$filenamestring
$options = 0intlibxml options
$namespace_or_prefix = ''string
$is_prefix = falsebool
returnsstatic

FromString($xml, $options, $namespace_or_prefix, $is_prefix)

staticXMLElement.php:24
static function FromString( string $xml, int $options = 0, string $namespace_or_prefix = '', bool $is_prefix = false ): static
$xmlstringif it doesn't already start with an XML declaration, one (UTF-8) is prepended
$options = 0intlibxml options
$namespace_or_prefix = ''string
$is_prefix = falsebool
returnsstatic

getAllChildren()

XMLElement.php:216
function getAllChildren(): array
returnsarrayevery direct child element, across every namespace used

getAttribute($name)

XMLElement.php:340

Get attribute and prefixed attribute's value as a string.

function getAttribute(string $name): ?string
$namestring
returnsnull | string
<movie r:id="25" title="my movie"> getAttribute('r:id'): "25" getAttribute('title): "my movie" getAttribute('test'): null

getAttributes($namespace, $isPrefix)

XMLElement.php:308
function getAttributes(string|true|null $namespace = true, bool $isPrefix = false): array
$namespace = truestring | true | null
$isPrefix = falsebool
returnsarray
$node->getAttributes(); // every attribute across every namespace, e.g. ['r:id' => '25', 'title' => 'my movie']

getBoolAttribute($name)

XMLElement.php:357

Get boolean attribute (prefix supported).

function getBoolAttribute(string $name): ?boolValue of attribute can either be 1 or true (case-insensitive).
$namestring
returnsnull | bool

getChild($tag)

XMLElement.php:203
function getChild(string $tag): ?self
$tagstringa plain tag name, or a 'prefix:tag' for a namespaced one
returnsnull | selfthe first direct child with this tag, or null if none matches
$node->getChild('foo:a');

getName($withPrefix)

XMLElement.php:62

Override so you can get the name with the prefix

function getName(bool $withPrefix = false): string
$withPrefix = falsebool
returnsstring
$node->getName(); // 'a', for a <foo:a> element$node->getName(true); // 'foo:a'

getNextSibling()

XMLElement.php:46
function getNextSibling(): ?self
returnsnull | selfthe next sibling element, or null if this is the last one

getParentNode()

XMLElement.php:41
function getParentNode(): self
returnsselfthe parent element (via an XPath 'parent::*' query)

getPreviousSibling()

XMLElement.php:51
function getPreviousSibling(): ?self
returnsnull | selfthe previous sibling element, or null if this is the first one

removeAttribute($name)

XMLElement.php:407

Remove attribute (prefix supported)

function removeAttribute(string $name): self
$namestring
returnsself

reorderNodes($fn)

XMLElement.php:171

Reorder nodes using a function

function reorderNodes(callable $fn): self
$fncallablefunction(XMLElement $a, XMLElement $b): int
returnsself

reorderNodesByTag($tagOrder)

XMLElement.php:187

Reorders child nodes according to a given tag order list.

function reorderNodesByTag(array $tagOrder): self
$tagOrderarrayOrdered list of tag names, e.g. ['title', 'author', 'year']
returnsselfA new SimpleXMLElement with children in the specified order

setAttribute($name, $value)

XMLElement.php:381

Set attribute's value (prefix supported).

function setAttribute(string $name, string $value): self
$namestring
$valuestring
returnsself

setAttributes($attributes)

XMLElement.php:368

Set multiple attributes (prefix supported)

function setAttributes(array $attributes): self
$attributesarray
returnsself

setInnerText($text)

XMLElement.php:92

Set inner text and replace all children by the text

function setInnerText(string $text): self
$textstring
returnsself
$node->setInnerText('hello');

toXML($declaredNs, $indent)

XMLElement.php:122

Export node to xml

function toXML(array $declaredNs = [], ?int $indent = 0): string
$declaredNs = []arrayNamespaces already declared in ancestor elements
$indent = 0null | intCurrent indentation level. if null print everything on same line
returnsstring
$node->toXML(); // pretty-printed, 2-space indented, without an XML declaration$node->toXML([], null); // everything on one line
Inherited members (21)
MethodsDeclared byDescription
addAttribute($qualifiedName, $value, $namespace)SimpleXMLElement
addChild($qualifiedName, $value, $namespace)SimpleXMLElement
asXML($filename)SimpleXMLElement
attributes($namespaceOrPrefix, $isPrefix)SimpleXMLElement
children($namespaceOrPrefix, $isPrefix)SimpleXMLElement
count()SimpleXMLElement
current()SimpleXMLElement
getChildren()SimpleXMLElement
getDocNamespaces($recursive, $fromRoot)SimpleXMLElement
getNamespaces($recursive)SimpleXMLElement
hasChildren()SimpleXMLElement
key()SimpleXMLElement
next()SimpleXMLElement
registerXPathNamespace($prefix, $namespace)SimpleXMLElement
rewind()SimpleXMLElement
saveXML($filename)SimpleXMLElement
valid()SimpleXMLElement
xpath($expression)SimpleXMLElement
__construct($data, $options, $dataIsURL, $namespaceOrPrefix, $isPrefix)SimpleXMLElement
__debugInfo()SimpleXMLElement
__toString()SimpleXMLElement