Element
Element
XML element node model.
class Element implements \ArrayAccess , \IteratorImplements: ArrayAccess, Iterator
XML document creation and parsing has been greatly simplified with the class. Documents can be created rapidly and will full support for attributes and namespaces.
$xml = new Hazaar\XML\Element('list');
$xml->properties['name'] = 'myProperties';
$xml->properties->add('property', 'Property #1');
$xml->properties->add('property', 'Property #2');
echo $xml;The above example will output the following XML:
<?xml version="1.0" encoding="utf-8" ?>
<list>
<properties name="myProperties">
<property>
Property #1
</property>
<property>
Property #2
</property>
</properties>
</list>Properties
__parent
public self $__parentType: self
__name
public string $__nameType: string
__namespace
public string $__namespaceType: string
__defaultNamespace
public string $__defaultNamespaceType: string
__namespaces
An array of namespaces defined on this node.
protected array $__namespacesType: array
__attributes
An array of attributed defined on this node.
protected array $__attributesType: array
__children
An array to contain all the child nodes of this node element.
protected array $__childrenType: array
__childrenIndex
protected $__childrenIndex__outputOptions
private Element\Options $__outputOptionsType: Hazaar\XML\Element\Options
__value
If there are no child nodes, the value that will used for this node element.
private mixed $__valueType: mixed
__reset
Toggle to indicate if the Element is being accessed as an array and is reset.
private bool $__reset = trueType: bool
Methods
__construct
Creates a new \Hazaar\XML\Element object.
public __construct(?string $name, mixed $value, ?array $namespaces, array|Element\Options $ouputOptions): voidParameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The name of the element to create, optionally including a namespace |
$value | mixed | The value of the element. This can be pretty much anything, including another \Hazaar\XML\Element object. |
$namespaces | array | Array of namespaces to declare where the key is the prefix and the value is the namespace URI |
$ouputOptions | array | Hazaar\XML\Element\Options |
__clone
Clone this element and recursively clone all child nodes.
public __clone(): voidReturns: void
Ensures cloned trees are fully detached from the original child object references.
__toString
Magic method to return the current node element as a string.
public __toString(): stringReturns: string
__isset
Checks if a child element with the given name exists.
public __isset(string $name): boolReturns: bool
This magic method is triggered by calling isset() on inaccessible or non-existing properties.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | the name of the child element to check |
__get
Magic method to access a child element by it's name.
public __get(string $name): array|self|nullReturns: array|self|null
This will automatically create a new child element when accessing a node that does not yet exist.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | the name of the child element to return |
__set
Magic method to automatically create a new child element on access.
public __set(string $name, mixed $value): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | the name key of child node to modify |
$value | mixed | he value to set |
fromString
Parse an XML string into an Element instance.
public fromString(string $xml): selfReturns: self
Parameters
| Parameter | Type | Description |
|---|---|---|
$xml | string | the XML string to parse |
setTagChars
Set the open and close tag characters for parsing and generating XML.
public setTagChars(string $openTag, string $closeTag): voidReturns: void
By changing the open and close tags we are able to use the \Hazaar\XML\Element class to work with text formats other than XML but that have a similar format.
Parameters
| Parameter | Type | Description |
|---|---|---|
$openTag | string | the open tag to set |
$closeTag | string | the close tag to set |
setDefaultNamespace
Sets the default namespace to use when accessing child nodes as members.
public setDefaultNamespace(string $ns): boolReturns: bool
The namespace MUST be a valid namespace already defined in the objects. If the namespace is not defined then FALSE will be returned. The namespace can be referenced with by it's alias or it's URL. In the latter case the alias will be looked up. Also, if the URL is not found the shortcut URL will be tried which is the URL with a colon appended (eg: DAV:).
Parameters
| Parameter | Type | Description |
|---|---|---|
$ns | string | the name/url of the namespace to use/prefix as default |
getDefaultNamespace
Returns the default namespace for the current node object.
public getDefaultNamespace(): ?stringReturns: string
The default namespace does not need to be set on the current object and can be set on a parent object. This method will look at it's own default namespace and if one is not set it will then request one from the parent.
addNamespace
Declare a namespace on the current node element.
public addNamespace(string $prefix, string $value): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$prefix | string | a namespace prefix |
$value | string | the value of the namespace |
namespaceExists
Test if a namespace is available for the current node element.
public namespaceExists(string $prefix): boolReturns: bool
This is a recursive test, meaning if the namespace isn't defined on the current element then it's parent will be checked. This is because namespaces, once declared, are available to all child elements of the node the namespace is declared on.
Parameters
| Parameter | Type | Description |
|---|---|---|
$prefix | string | the namespace prefix to check |
attr
Set an attribute on the current Hazaar\XML\Element object.
public attr(string $name, string $value): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | the name of the attribute |
$value | string | the value of the attribute |
getAttr
Return an attribute value.
public getAttr(string $name): stringReturns: string
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The name of the attribute to return |
attributes
Returns a list of attributes current declared on the node element.
public attributes(): arrayReturns: array
add
Adds a new element to the current node element.
public add(string $name, mixed $value, ?array $namespaces): selfReturns: self
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The name of the element to create, optionally including a namespace |
$value | mixed | The value of the element. This can be pretty much anything, including another \Hazaar\XML\Element object. |
$namespaces | array | Array of namespaces to declare where the key is the prefix and the value is the namespace URI |
addChild
Adds a child Element to the current Element.
public addChild(self $element): selfReturns: self
Sets the parent of the given Element to this Element, manages namespaces if necessary, updates the list of children and the children index for quick lookup.
Parameters
| Parameter | Type | Description |
|---|---|---|
$element | self | the child Element to add |
addFromArray
Creates and adds child elements to the current XML element from an array.
public addFromArray(string $name, array $array, string $tag = 'child'): selfReturns: self
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | the name of the current XML element |
$array | array | the array containing the data for creating child elements |
$tag | string | the tag name for the child elements (default: 'child') |
child
Returns a child by it's node name.
public child(string $name, mixed $value, ?array $namespaces): array|selfReturns: array|self
If the node doesn't exist, then it is automatically added.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The name of the element to create, optionally including a namespace |
$value | mixed | The value of the element. This can be pretty much anything, including another Hazaar\XML\Element object. |
$namespaces | array | Array of namespaces to declare where the key is the prefix and the value is the namespace URI |
children
Returns a list of all child elements on the current node element.
public children(): arrayReturns: array
count
Returns the number of children on the current node element.
public count(): intReturns: int
hasChildren
Returns the TRUE or FALSE to indicate that the node has child elements.
public hasChildren(): boolReturns: bool
getName
Get the full valid name of the current node element.
public getName(bool $includeInvalid_namespace): stringReturns: string
This will return the full name which includes the namespace that was originally defined ONLY if the namespace is valid. Namespaces are valid if they have also been defined with a call to Hazar\Xml\Element::addNamespace() on this element or a parent element.
If the namespace is NOT valid, then it is simply ignored. If the namespace should be returned regardless of it's validity, use the $includeInvalid_namespace parameter.
Parameters
| Parameter | Type | Description |
|---|---|---|
$includeInvalid_namespace | bool | include namespaces that have no been defined in the current or parent nodes |
getLocalName
Get the local name of the XML element.
public getLocalName(): stringReturns: string
getNamespace
Get the namespace of the XML element.
public getNamespace(): ?stringReturns: string
value
Get or set the current value of the node element.
public value(?string $value): mixedReturns: mixed
If the node element does not have any child nodes then it's value can be set directly. If there are child nodes defined then this value is ignored when generating XML output using the Hazaar\XML\Element::toString() method.
If the $value parameter is not defined then current value is returned without modification
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | string | The value to set. If this is null then the value will not be modified. To empty the value set it to an empty string. |
Unsets the value and children of the XML element.
public (): voidReturns: void
This method resets the element by setting its value to null and clearing both the children array and the children index array.
getNamespaces
Returns an array of namespaces currently defined on the current node element.
public getNamespaces(bool $recursive): arrayReturns: array
Optionally return any namespaces defined anywhere in the data object model.
Parameters
| Parameter | Type | Description |
|---|---|---|
$recursive | bool | recurse through all child node elements and append their namespace declarations to the list |
toString
Output the current XML structure as a string or save to file.
public toString(?string $filename): bool|stringReturns: bool|string
This method generates the current XML structure as a returns it as a string resolving all names, namespaces and child elements.
Parameters
| Parameter | Type | Description |
|---|---|---|
$filename | string | optional filename to save XML directly into |
loadXML
Load an XML definition from a string.
public loadXML(string $xml): boolReturns: bool
The Hazaar\XML\Element class can not only be used to generate XML, but also parse it to allow programmatic access to the data structure. This method takes a single string argument and attempts to parse it as valid XML.
Parameters
| Parameter | Type | Description |
|---|---|---|
$xml | string | The XML source string |
hasChild
Tests if a child element exists on the current node.
public hasChild(string $name): boolReturns: bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The name of the child element with optional namespace |
hasAttr
Tests if an attribute exists on the current node.
public hasAttr(string $name): voidParameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The name of the child element with optional namespace |
find
Searches children to find an element that matches search criteria.
public find(array $criteria, ?string $name): ?selfReturns: self
Parameters
| Parameter | Type | Description |
|---|---|---|
$criteria | array | An array of attribute criteria to search on. Example: ['name' => 'test'] will find elements who have a name attribute with a value of 'test'. |
$name | string | optional node name to filter on |
offsetSet
Set element attribute.
public offsetSet(mixed $name, mixed $value): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | mixed | the name key of child node to modify |
$value | mixed | the value to set |
offsetExists
Test if element attribute exists.
public offsetExists(mixed $name): boolReturns: bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | mixed | the name key of child node to check |
offsetGet
Return an attribute value.
public offsetGet(mixed $name): mixedReturns: mixed
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | mixed | The name of the attribute to return |
offsetUnset
Unset an elements attribute.
public offsetUnset(mixed $name): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | mixed | the key name of the child to unset |
current
Return the current child element.
public current(): mixedReturns: mixed
next
Move to the next child element.
public next(): voidReturns: void
key
Return the key of the current child element.
public key(): mixedReturns: mixed
valid
Test if the current child element is valid.
public valid(): boolReturns: bool
rewind
Reset the internal pointer to the first child element.
public rewind(): voidReturns: void
removeIndex
Returns the child element at the specified index.
public removeIndex(int $index): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$index | int | The index of the child element to return |
getIndex
Returns the child element at the specified index.
public getIndex(int $index): mixedReturns: mixed
Parameters
| Parameter | Type | Description |
|---|---|---|
$index | int | The index of the child element to return |
setOutputOptions
Sets the output options for the XML element.
public setOutputOptions(array|Element\Options $options): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$options | array | Hazaar\XML\Element\Options |
setName
Sets the name of the current \Hazaar\XML\Element object.
protected setName(string $name): voidReturns: void
Namespaces can be specifed using the colon separator. eg: 'namespace:name'.
$node->setName('C:mynode');Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | the name to set for the current object, optionally including a namespace |
setParent
Sets the parent object for the current Hazaar\XML\Element object.
private setParent(self $parent): voidReturns: void
This is an internal method and is not accessible outside of the Hazaar\XML\Element class.
Parameters
| Parameter | Type | Description |
|---|---|---|
$parent | self | the Hazaar\XML\Element object to use as the parent |
resolveXML
Recursively resolves an XML element or an array of elements to their XML string representation.
private resolveXML(array|self $element): stringReturns: string
If the input is an array of elements, this method concatenates the XML representations of each element. If the input is a single Element, it returns its XML string.
Parameters
| Parameter | Type | Description |
|---|---|---|
$element | array | self |
Generated by Hazaar API Doc Generator on Fri, 04 Sep 2026 23:32:18 +0000