public class XMLElement

class.xmlelement.php #12

XMLElement is a class used to simulate PHP's DOMElement class. Each object is a representation of a HTML element and can store it's children in an array. When an XMLElement is generated, it is output as an XML string.

Methods

void __construct(string $name, string|XMLElement $value, array $attributes, boolean $createHandle)

The constructor for the XMLElement

Parameters
  • $name stringThe name of the XMLElement, 'p'.

  • $value string|XMLElement(optional) The value of this XMLElement, it can be a string or another XMLElement object.

  • $attributes array(optional) Any additional attributes can be included in an associative array with the key being the name and the value being the value of the attribute. Attributes set from this array will override existing attributes set by previous params.

  • $createHandle booleanWhether this function should convert the $name to a handle. Defaults to false.

void addClass(string $class)

A convenience method to quickly add a CSS class to this XMLElement's existing class attribute. If the attribute does not exist, it will be created.

Parameters
  • $class stringThe CSS classname to add to this XMLElement

void addProcessingInstruction(string $pi)

Adds processing instructions to this XMLElement

boolean appendChild(XMLElement|string $child)

Adds an XMLElement to the children array

void appendChildArray(array $children)

A convenience method to add children to an XMLElement quickly.

static XMLElement convertFromDOMDocument(string $root_element, DOMDOcument $doc)

Given a DOMDocument, this function will convert it to an XMLElement object and return the result.

static XMLElement convertFromXMLString(string $root_element, string $xml)

Given a string of XML, this function will convert it to an XMLElement object and return the result.

Parameters
  • $xml stringA string of XML

string generate(boolean $indent, integer $tab_depth, boolean $has_parent)

This function will turn the XMLElement into a string representing the element as it would appear in the markup. The result is valid XML.

Parameters
  • $indent booleanDefaults to false

  • $tab_depth integerDefaults to 0, indicates the number of tabs (\t) that this element should be indented by in the output string

  • $has_parent booleanDefaults to false, set to true when the children are being generated. Only the parent will output an XML declaration if $this->_includeHeader is set to true.

string getAttribute(string $name)

Retrieves the value of an attribute by name

array getAttributes()

Accessor for $this->_attributes

XMLElement getChild(integer $position)

Retrieves a child-element by position

XMLElement getChildByName(string $name, integer $position)

Retrieves child-element by name and position. If no child is found, NULL will be returned.

array getChildren()

Accessor for $this->_children

array getChildrenByName(string $name)

Accessor to return an associative array of all $this->_children whose's name matches the given $name. If no children are found, an empty array will be returned.

Returns

An associative array where the key is the $index of the child in $this->_children

XMLElementChildrenFilter getIterator()

Accessor for $this->_children, returning only XMLElement children, not text nodes.

string getName()

Accessor for $_name

integer getNumberOfChildren()

Returns the number of children this XMLElement has.

string|XMLElement getValue()

Accessor for $_value

boolean insertChildAt(integer $index, XMLElement $child)

Given a desired index, and an XMLElement, this function will insert the child at that index in $this->_children shuffling all children greater than $index down one. If the $index given is greater then the number of children for this XMLElement, the $child will be appended to the current $this->_children array.

Parameters
  • $index integerThe index where the $child should be inserted. If this is negative the index will be calculated from the end of $this->_children.

  • $child XMLElementThe XMLElement to insert at the desired $index

void prependChild(XMLElement $child)

Adds an XMLElement to the start of the children array, this will mean it is output before any other children when the XMLElement is generated

boolean removeChildAt(integer $index)

Given the position of the child in the $this->_children, this function will unset the child at that position. This function is not reversible. This function does not alter the key's of $this->_children after removing a child

Parameters
  • $index integerThe index of the child to be removed. If the index given is negative it will be calculated from the end of $this->_children.

Returns

true if child was successfully removed, false otherwise.

void removeClass(string $class)

A convenience method to quickly remove a CSS class from an XMLElement's existing class attribute. If the attribute does not exist, this method will do nothing.

Parameters
  • $class stringThe CSS classname to remove from this XMLElement

boolean replaceChildAt(integer $index, XMLElement $child)

Given the position of the child to replace, and an XMLElement of the replacement child, this function will replace one child with another

Parameters
  • $index integerThe index of the child to be replaced. If the index given is negative it will be calculated from the end of $this->_children.

  • $child XMLElementAn XMLElement of the new child

void replaceValue(string|XMLElement|array $value)

This function will remove all text attributes from the XMLElement node and replace them with the given value.

void setAllowEmptyAttributes(bool|string $value)

Specifies whether attributes need to have a value or if they can be shorthand on this XMLElement.

Parameters
  • $value bool|string(optional) Defaults to true

void setAttribute(string $name, string $value)

Sets an attribute

Parameters
  • $name stringThe name of the attribute

  • $value stringThe value of the attribute

void setAttributeArray(array $attributes)

A convenience method to quickly add multiple attributes to an XMLElement

Parameters
  • $attributes arrayAssociative array with the key being the name and the value being the value of the attribute.

boolean setChildren(array $children)

This function expects an array of XMLElement that will completely replace the contents of $this->_children. Take care when using this function.

Parameters
  • $children arrayAn array of XMLElement's to act as the children for the current XMLElement instance

void setDTD(string $dtd)

Sets the DTD for this XMLElement

void setElementStyle(string $style)

Sets the style of the XMLElement. Used when the XMLElement is being generated to determine whether needs to be closed, is self closing or is standalone.

Parameters
  • $style string(optional) Defaults to 'xml', any other value will trigger the XMLElement to be closed by itself or left standalone if it is in the XMLElement::no_end_tags.

void setEncoding(string $value)

Sets the encoding for this XMLElement for when it's generated.

void setIncludeHeader(bool|string $value)

Sets whether this XMLElement needs to output an XML declaration or not. This normally is only set to true for the parent XMLElement, eg. 'html'.

Parameters
  • $value bool|string(optional) Defaults to false

void setName(string $name, boolean $createHandle)

Sets the name of this XMLElement, ie. 'p' =>

Parameters
  • $name stringThe name of the XMLElement, 'p'.

  • $createHandle booleanWhether this function should convert the $name to a handle. Defaults to false.

void setSelfClosingTag(bool|string $value)

Sets whether this XMLElement is self closing or not.

Parameters
  • $value bool|string(optional) Defaults to true

void setValue(string|XMLElement|array $value)

Sets the value of the XMLElement. Checks to see whether the value should be prepended or appended to the children.

Parameters
  • $value string|XMLElement|arrayDefaults to true.

void setVersion(string $value)

Sets the version for the XML declaration of this XMLElement

static string stripInvalidXMLCharacters(string $value)

This function strips characters that are not allowed in XML

See Also

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details