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:

pre. <?xml version="1.0" encoding="utf-8" ?>

<properties name="myProperties">
   <property>
       Property #1
   </property>
   <property>
        Property #2
    </property>
</properties>

Summary
MethodsPropertiesConstants
Constants
ENCODING
ENCODING : type
Encoding format to use when generating XML as a string

Type

\type - desc
VERSION
VERSION : type
Current supported XML version

Type

\type - desc
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
__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.
__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.
__toString()
__toString() : string
Magic method to return the current node element as a string
add()
add($name, $value = null, $namespaces = null) : \Hazaar\Xml\Element
Adds a new element to the current node element.
addNamespace()
addNamespace($prefix, $value)
Declare a namespace on the current node element
attr()
attr($name, $value)
Set an attribute on the current Hazaar\XML\Element object.
attributes()
attributes() : array
Returns a list of attributes current declared on the node element.
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.

children()
children() : array
Returns a list of all child elements on the current node element
count()
count() : integer
Returns the number of children on the current node element
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
getAttr()
getAttr($name) : string
Return an attribute value
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.

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.

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.

hasAttr()
hasAttr($name) : boolean
Tests if an attribute exists on the current node
hasChild()
hasChild($name) : boolean
Tests if a child element exists on the current node
hasChildren()
hasChildren() : integer
Returns the TRUE or FALSE to indicate that the node has child elements
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.

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.

next()
next()
Move to the next child element
offsetExists()
offsetExists($name) : boolean
Test if element attribute exists
offsetGet()
offsetGet($name) : string
Return an attribute value
offsetSet()
offsetSet($name, $value)
Set element attribute
offsetUnset()
offsetUnset($name)
Unset an elements attribute
resolveXML()
resolveXML($element)
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:).

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');
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.

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.

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.

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