\Hazaar\Xml
Element
Represents an element in an XML document.

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 <a href="http://1">#1</a>
       </property>
       <property>
            Property <a href="http://2">#2</a>
        </property>
    </properties>
</list>

Tags

Since

2.0.0

Summary
Methods Properties Constants
add
key
Constants
ENCODING
ENCODING
Encoding format to use when generating XML as a string
VERSION
VERSION
Current supported XML version
Properties
$__attributes
$__attributes
An array of attributed defined on this node
$__children
$__children
An array to contain all the child nodes of this node element
$__children_index
$__children_index
$__default_namespace
$__default_namespace
The default namespace prefix to use when accessing child members.
$__name
$__name
The name of the current node element
$__namespace
$__namespace
The namespace prefix for the current node element
$__namespaces
$__namespaces
An array of namespaces defined on this node
$__parent
$__parent
The direct parent object of the current node element
$__reset
$__reset
Toggle to indicate if the Element is being accessed as an array and is reset.
$__value
$__value
If there are node child nodes, the value that will used for this node element.
$close_tag
$close_tag
$open_tag
$open_tag
Methods
__clone()
__clone()
__construct()
__construct($name = null, $value = null, $namespaces = null, $open_tag = '<', $close_tag = '>')
Creates a new \Hazaar\XML\Element object

Tags

Since

2.0.0.0

Parameters

$namestring

The name of the element to create, optionally including a namespace

$valuemixed

The value of the element. This can be pretty much anything, including another \Hazaar\Xml\Element object.

$namespacesarray

Array of namespaces to declare where the key is the prefix and the value is the namespace URI

$open_tagstring

Configurable open tag for elements. XML defines this as ‘<’. This can be changed for use with alternative file formats.

$close_tagstring

Configurable close tag for elements. XML defines this as ‘>’. This can be changed for use with alternative file formats.

__get()
__get($name) : \Hazaar\Xml\Element
Magic method to access a child element by it's name.

This will automatically create a new child element when accessing a node that does not yet exist.

This only works when not working with namespaces.

Tags

Since

2.0.0

Parameters

$namestring

The name of the child element to return.

Returns

\Hazaar\Xml\Element

The child element being requested.

__set()
__set($name, $value) : \Hazaar\Xml\Element
Magic method to automatically create a new child element on access
This only works when not working with namespaces.

Tags

Since

2.0.0

Parameters

$namestring

The name key of child node to modify.

$valuestring

he value to set.

Returns

\Hazaar\Xml\Element

Returns the child node being modified.

__toString()
__toString() : string
Magic method to return the current node element as a string

Tags

Since

2.0.0.0

Returns

string
add()
add($name, $value = null, $namespaces = null) : \Hazaar\Xml\Element
Adds a new element to the current node element.

Tags

Since

2.0.0.0

Parameters

$namestring

The name of the element to create, optionally including a namespace

$valuemixed

The value of the element. This can be pretty much anything, including another \Hazaar\Xml\Element object.

$namespacesarray

Array of namespaces to declare where the key is the prefix and the value is the namespace URI

Returns

\Hazaar\Xml\Element

The new child object.

addNamespace()
addNamespace($prefix, $value)
Declare a namespace on the current node element

Parameters

$prefixstring

A namespace prefix.

$valuestring

The value of the namespace.

attr()
attr($name, $value)
Set an attribute on the current Hazaar\XML\Element object.

Tags

Since

2.0.0.0

Parameters

$namestring

The name of the attribute.

$valuestring

The value of the attribute.

attributes()
attributes() : array
Returns a list of attributes current declared on the node element.

Tags

Since

2.0.0.0

Returns

array
child()
child($name, $value = null, $namespaces = null) : \Hazaar\Xml\Element
Returns a child by it's node name.

If the node doesn’t exist, then it is automatically added.

Tags

Since

2.0.0.0

Parameters

$namestring

The name of the element to create, optionally including a namespace

$valuemixed

The value of the element. This can be pretty much anything, including another Hazaar\Xml\Element object.

$namespacesarray

Array of namespaces to declare where the key is the prefix and the value is the namespace URI

Returns

\Hazaar\Xml\Element
children()
children() : array
Returns a list of all child elements on the current node element

Tags

Since

2.0.0.0

Returns

array
count()
count() : integer
Returns the number of children on the current node element

Tags

Since

2.0.0.0

Returns

integer
current()
current()
Return the current child element
find()
find($criteria, $name = null) : \Hazaar\Xml\Element
Searches children to find an element that matches search criteria

Tags

Since

2.0.1

Parameters

$criteriaarray

An array of attribute criteria to search on. Example: array(‘name’ => ‘test’) will find elements who have a name attribute with a value of ‘test’.

$namestring

Optional node name to filter on.

Returns

\Hazaar\Xml\Element

The child element if found. NULL Otherwise.

getAttr()
getAttr($name) : string
Return an attribute value

Parameters

$namestring

The name of the attribute to return

Returns

string

The value of the attribute.

getDefaultNamespace()
getDefaultNamespace() : string
Returns the default namespace for the current node object

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.

Returns

string

The default namespace to use.

getLocalName()
getLocalName()
getName()
getName($include_invalid_namespace = false) : string
Get the full valid name of the current node element

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 $include_invalid_namespace parameter.

Tags

Since

2.0.0.0

Parameters

$include_invalid_namespaceboolean

Include namespaces that have no been defined in the current or parent nodes.

Returns

string
getNamespace()
getNamespace()
getNamespaces()
getNamespaces($recursive = false) : array
Returns an array of namespaces currently defined on the current node element

Optionally return any namespaces defined anywhere in the data object model.

Tags

Since

2.0.0.0

Parameters

$recursiveboolean

Recurse through all child node elements and append their namespace declarations to the list.

Returns

array

An array of defined namespaces with keys as the prefix and URIs as the value.

hasAttr()
hasAttr($name) : boolean
Tests if an attribute exists on the current node

Tags

Since

2.0.0

Parameters

$namestring

The name of the child element with optional namespace

Returns

boolean

True if the child element exists, false otherwise.

hasChild()
hasChild($name) : boolean
Tests if a child element exists on the current node

Tags

Since

2.0.0

Parameters

$namestring

The name of the child element with optional namespace

Returns

boolean

True if the child element exists, false otherwise.

hasChildren()
hasChildren() : integer
Returns the TRUE or FALSE to indicate that the node has child elements

Tags

Since

2.0.0.0

Returns

integer
key()
key()
Return the key of the current child element
loadXML()
loadXML($xml) : boolean
Load an XML definition from a string

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.

Tags

Since

2.0.0.0

Parameters

$xmlstring

The XML source string

Returns

boolean

Indicates success or failure

namespaceExists()
namespaceExists($prefix) : boolean
Test if a namespace is available for the current node element

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.

Tags

Since

2.0.0.0

Parameters

$prefixstring

The namespace prefix to check.

Returns

boolean
next()
next()
Move to the next child element
offsetExists()
offsetExists($name) : boolean
Test if element attribute exists

Parameters

$namestring

The name key of child node to check.

Returns

boolean

TRUE if the child node exists, FALSE otherwise.

offsetGet()
offsetGet($name) : string
Return an attribute value

Parameters

$namestring

The name of the attribute to return

Returns

string

The value of the attribute.

offsetSet()
offsetSet($name, $value)
Set element attribute

Parameters

$namestring

The name key of child node to modify.

$valuemixed

The value to set.

offsetUnset()
offsetUnset($name)
Unset an elements attribute

Parameters

$namestring

The key name of the child to unset.

resolveXML()
resolveXML($element)

Parameters

$element

No description

rewind()
rewind()
Reset the internal pointer to the first child element.
setDefaultNamespace()
setDefaultNamespace($ns) : boolean
Sets the default namespace to use when accessing child nodes as members

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

$nsstring

The name/url of the namespace to use/prefix as default.

Returns

boolean
setName()
setName($name)
Sets the name of the current \Hazaar\XML\Element object.

Namespaces can be specifed using the colon separator. eg: ‘namespace:name’.

$node->setName(‘C:mynode’);

Tags

Since

2.0.0.0

Parameters

$namestring

The name to set for the current object, optionally including a namespace.

setParent()
setParent($parent)
Sets the parent object for the current Hazaar\Xml\Element object.

This is an internal method and is not accessible outside of the Hazaar\Xml\Element class.

Tags

Since

2.0.0.0

Parameters

$parent\Hazaar\Xml\Element

The Hazaar\Xml\Element object to use as the parent.

setTagChars()
setTagChars($open_tag, $close_tag)
Set the open and close tag characters for parsing and generating XML.

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

$open_tagstring

The open tag to set.

$close_tagstring

The close tag to set.

toXML()
toXML($filename = null) : Mixed
Output the current XML structure as a string or save to file.

This method generates the current XML structure as a returns it as a string resolving all names, namespaces and child elements.

Tags

Since

2.0.0.0

Parameters

$filenamestring

Optional filename to save XML directly into.

Returns

Mixed

If saving to file, returns true/false indicating the file write result. Otherwise returns the XML as a string.

valid()
valid()
Test if the current child element is valid
value()
value($value = null) : string
Get or set the current value of the node element

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::toXML() method.

If the $value parameter is not defined then current value is returned without modification

Tags

Since

2.0.0.0

Parameters

$valuestring

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.

Returns

string

The current value of the element.