Index

XmlNode

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

class

XmlNode

XmlNode.php:18

An XmlNodes with a tag name and attributes -- an actual XML/HTML element. Attribute access supports an optional namespace prefix, and can also go through array access using an '@'-prefixed key ($node['@class'] reads/writes the 'class' attribute, alongside $node[0] etc. for children).

class MMO\core\helpers\XmlNode extends MMO\core\helpers\XmlNodes
Namespace
MMO\core\helpers
Extends
XmlNodes
Todo
replace property getter with actual public properties
you can access all properties (use tPropertyGetter)
$div = new XmlNode('div', ['class' => 'box'], ['Hello']); (string)$div; // '<div class="box">Hello</div>' $div['@class']; // 'box'

Index

attributes
tagName
addCSSClass($class)Add one or more (space-separated) classes to the 'class' attribute, without duplicating ones already present.
closest($tag)Walk up the ancestor chain (starting at the parent, all the way up to and including the document root) looking for the first one whose tag matches $tag.
debugName()
From($xml, $simplify) static
getAttribute($name, $ns)
getAttributes()
hasAttribute($name, $ns)
hasCSSClass($class)
isSelfClosing()
IsValidTag($tag) static
nextElementSibling()
nextSibling()
offsetExists($key)\ArrayAccess::offsetExists -- a '@name' key checks that attribute; anything else falls through to
offsetGet($key)\ArrayAccess::offsetGet -- a '@name' key reads that attribute; anything else falls through to
offsetSet($key, $value)\ArrayAccess::offsetSet -- a '@name' key sets that attribute; anything else falls through to
offsetUnset($key)\ArrayAccess::offsetUnset -- a '@name' key removes that attribute; anything else falls through to
removeAttribute($name)
removeCSSClass($class)
render($opts)
renderTag()
setAttribute($key, $value)Set attribute
setAttributes($attrs)Set several attributes at once (each via setAttribute).
toArray()
__construct($tag, $attrs, $nodes)

Properties2

attributes

protected array $attributes = []
Type
array

tagName

string $tagName
Type
string

Methods24

addCSSClass($class)

XmlNode.php:200

Add one or more (space-separated) classes to the 'class' attribute, without duplicating ones already present.

function addCSSClass(string $class)
$classstring
returns
$node->addCSSClass('active highlighted');

closest($tag)

XmlNode.php:66

Walk up the ancestor chain (starting at the parent, all the way up to and including the document root) looking for the first one whose tag matches $tag.

function closest(string $tag): ?MMO\core\helpers\XmlNode
$tagstring
returnsnull | XmlNode
$span->closest('div');

From($xml, $simplify)

staticXmlNode.php:37
static function From($xml, bool $simplify = true): self
$xml
$simplify = truebool
returnsself
throwsalways -- parsing produces a whole tree (possibly several elements), not a single pre-tagged XmlNode; use XmlNodes::From instead

getAttributes()

XmlNode.php:139
function getAttributes(): array
returnsarrayevery attribute, name => value

hasAttribute($name, $ns)

XmlNode.php:107
function hasAttribute(string $name, ?string $ns = null): bool
$namestring
$ns = nullnull | stringnull: check the plain attribute name; '*': check any namespace (matches an attribute stored as 'anyns:$name'); a specific string: check that exact namespace
returnsbool

hasCSSClass($class)

XmlNode.php:188
function hasCSSClass(string $class): bool
$classstring
returnsbooltrue if the 'class' attribute contains $class as one of its space-separated tokens

IsValidTag($tag)

staticXmlNode.php:42
static function IsValidTag(string $tag): bool
$tagstring
returnsbooltrue if $tag contains only alphanumeric characters, '-', '_' or ':'

nextElementSibling()

XmlNode.php:82
function nextElementSibling(): ?MMO\core\helpers\XmlNodes
returnsnull | XmlNodesthe next sibling that is itself an XmlNodes/XmlNode instance, skipping over text nodes

nextSibling()

XmlNode.php:74
function nextSibling(): mixed
returnsmixedthe next sibling (a string or an XmlNodes/XmlNode instance), or null if this is the last child

offsetExists($key)

XmlNode.php:270

\ArrayAccess::offsetExists -- a '@name' key checks that attribute; anything else falls through to

function offsetExists(mixed $key): bool
See
XmlNodes::offsetExists for children.
$keymixed
returnsbool
isset($node['@class']);

offsetGet($key)

XmlNode.php:246

\ArrayAccess::offsetGet -- a '@name' key reads that attribute; anything else falls through to

function offsetGet(mixed $key): mixed
See
XmlNodes::offsetGet for children. Returned by reference (so e.g. $node['@class'][] = 'box'; can mutate an array-valued attribute in place) -- for an attribute that isn't set, this returns a reference to a fresh, unshared null instead of indexing $attributes directly: doing the latter would have PHP silently auto-vivify that key (creating a real, permanent null-valued attribute as a side effect of merely reading it) -- which then renders as a stray bare attribute, since setAttribute treats a null value as one.
$keymixed
returnsmixed
$node['@class'];

offsetSet($key, $value)

XmlNode.php:260

\ArrayAccess::offsetSet -- a '@name' key sets that attribute; anything else falls through to

function offsetSet(mixed $key, mixed $value): void
See
XmlNodes::offsetSet for children.
$keymixed
$valuemixed
returnsvoid
$node['@class'] = 'box';

offsetUnset($key)

XmlNode.php:231

\ArrayAccess::offsetUnset -- a '@name' key removes that attribute; anything else falls through to

function offsetUnset(mixed $key): void
See
XmlNodes::offsetUnset for children.
$keymixed
returnsvoid
unset($node['@class']);

removeCSSClass($class)

XmlNode.php:212
function removeCSSClass(string $class): bool
$classstring
returnsbooltrue if $class was present (and has now been removed)

setAttribute($key, $value)

XmlNode.php:154

Set attribute

function setAttribute(string $key, mixed $value): void
$keystring
$valuemixedan array is later rendered space-joined (e.g. for 'class'); null renders as a bare attribute with no ="..." value at all (e.g. HTML's disabled)
returnsvoid

__construct($tag, $attrs, $nodes)

XmlNode.php:27
function __construct($tag, $attrs = [], $nodes = [])
$tag
$attrs = []name => value
$nodes = []initial children, see XmlNodes::add
Inherited members (40)
PropertiesDeclared byDescription
aliasXmlNodes
childNodesXmlNodes
ClosingTagsXmlNodesHTML void elements: self-closing, never get a separate closing tag when rendered.
parentNodeXmlNodes
MethodsDeclared byDescription
add($value, $alias, $first)XmlNodesAdd value as a node Convert any array into a HTMLNodes
addXml($xml)XmlNodesParse xml and add nodes
children()XmlNodes
clear()XmlNodesRemove every child (and alias), detaching each from this tree.
contains($node)XmlNodes
count()XmlNodes\Countable::count
create($tag, $attrs, $nodes)XmlNodesConvenience factory: build a new node (not yet attached to this tree).
FromFile($filename, $simplify)XmlNodesSame as From, reading the XML/HTML from a file.
getAlias($node)XmlNodes
getContent()XmlNodes
getElementByTagName($tag, $ns)XmlNodesGet first element with the given tag
getElementsById($id)XmlNodes
getElementsByTagName($tag, $ns)XmlNodes
getIterator()XmlNodes\IteratorAggregate::getIterator
getParentNode()XmlNodes
getRootNode()XmlNodes
hasChildNodes()XmlNodes
hasElement($tag)XmlNodes
innerNodes($opts)XmlNodes
innerText()XmlNodes
isClosingTag($tag)XmlNodes
jsonSerialize()XmlNodes
listElementsByTagName($tags, $nodes)XmlNodesLike getElementsByTagName but for several tag names at once.
loopElementsByTagName($tag, $ns)XmlNodesLook for all children recursively who have a given tag
nearest($tags)XmlNodesWalk up the ancestor chain (starting at the parent, all the way up to and including the document root) looking for the first one whose tag matches.
push($key, $value)XmlNodesPush a value into an EXISTING child (by numeric index or alias) -- delegates to that child's own add (or, for two adjacent text nodes, string concatenation) -- or, if $key doesn't resolve to a…
remove()XmlNodesDetach this node from its parent (a no-op if it has none).
removeChild($node)XmlNodesDetach $node from this tree's direct children (and any alias pointing at it).
replaceChild($newNode, $oldNode)XmlNodesReplace $oldNode (a direct child) with $newNode in place, carrying over any alias that pointed to $oldNode (checked up the WHOLE ancestor chain, not just this node).
setAlias($alias, $node)XmlNodesName (or rename) a child so it can be looked up via $nodes['alias'] -- does NOT add $node as a child itself, see add for that.
setContent($content)XmlNodesReplace every child with $content (via clear then add).
setParentNode($parent)XmlNodesAttach this node to $parent, detaching it from its previous parent first if it had a different one.
stripTags($includes, $excludes)XmlNodes
__get($key)XmlNodes__get -- allow read access to a protected/private property from outside the class.
__isset($key)XmlNodes__isset
__toString()XmlNodes