Documentation

XmlNode
in package

Class for interacting with XML objects.

This class provides various properties to access and manipulate XML data and elements.

Table of Contents

Constants

XmlStart  = '<?xml version="1.0" encoding="%s"?>'
XML declaration start string.

Properties

$attributes  : XmlNodeAttributeList
$cdata  : string
$children  : XmlNodeList
$data  : string
$document  : DOMDocument
$elements  : XmlNodeList
$elementsCount  : int
$encoding  : string
$firstChild  : XmlNode
$html  : string
$innerHtml  : string
$innerXml  : string
$isCData  : bool
$name  : string
$next  : XmlNode
$nodes  : XmlNodeList
$parent  : XmlNode
$prev  : XmlNode
$raw  : DOMNode
$root  : XmlNode
$tag  : object
$texts  : XmlNodeList
$type  : string
$value  : string
$xml  : string
$_document  : DOMDocument|null
The raw document object.
$_node  : DOMNode|null
The raw element object.
$_tag  : object|null
Additional data.

Methods

__construct()  : mixed
Constructor.
__get()  : mixed
Getter method.
__set()  : void
Setter method.
Append()  : void
Adds the specified nodes/node to the end.
CreateTextNode()  : XmlNode
Creates a text node with the specified content.
Export()  : self
Exports the XML data with a specified document tag.
FromObject()  : XmlNode
Restores a node from an XmlSerialized object.
getElementsByName()  : XmlNamedNodeList
Returns elements with an attribute @name containing the specified name.
Insert()  : void
Adds the specified nodes/node before the $relation node.
IsChildOf()  : bool
Checks if the current node is a child of the specified node.
Item()  : XmlNode|null
Returns an XmlNode object corresponding to the child object with the name $name.
Items()  : XmlNodeList
Returns an XmlNodeList with the tag name $name.
Load()  : XmlNode
Creates an XmlNode object from a string or a file.
LoadHTML()  : XmlNode
Creates an XmlNode object from an HTML string or file.
LoadHtmlNode()  : XmlNode
Creates an XMLHtmlNode from an incomplete document.
LoadNode()  : XmlNode
Creates an XmlNode from an incomplete document.
Path()  : string
Returns the path based on the query.
Query()  : XmlNodeList|XmlNamedNodeList
Executes an XPath query.
Remove()  : void
Removes the current node.
ReplaceTo()  : void
Replaces the current node with the specified node.
Save()  : string|null
Saves the XML stored in the object to a file or returns it as a string.
SaveHTML()  : string|null
Saves the HTML stored in the object to a file or returns it as a string.
ToObject()  : XmlSerialized|XmlCData|string|null
Converts the current node and its children into an XmlSerialized object.

Constants

XmlStart

XML declaration start string.

public string XmlStart = '<?xml version="1.0" encoding="%s"?>'

This constant represents the starting string of an XML declaration, including the version and encoding information.

Properties

$cdata write-only

public string $cdata

The CDATA section of the XML object.

$data read-only

public string $data

The data of the XML object.

$document

public DOMDocument $document

The DOMDocument associated with the XML object.

$elementsCount read-only

public int $elementsCount

The number of child elements of the XML object.

$encoding read-only

public string $encoding

The encoding of the XML object.

$firstChild read-only

public XmlNode $firstChild

The first child node of the XML object.

$html read-only

public string $html

The HTML string of the XML object.

$innerHtml read-only

public string $innerHtml

The inner HTML string of the XML object.

$innerXml read-only

public string $innerXml

The inner XML string of the XML object.

$isCData read-only

public bool $isCData

Indicates whether the XML object is a CDATA section.

$name read-only

public string $name

The name of the XML object.

$next read-only

public XmlNode $next

The next sibling node of the XML object.

$parent read-only

public XmlNode $parent

The parent node of the XML object.

$prev read-only

public XmlNode $prev

The previous sibling node of the XML object.

$raw

public DOMNode $raw

The raw DOMNode associated with the XML object.

$tag

public object $tag

The tag object associated with the XML object.

$type read-only

public string $type

The type of the XML object.

$value

public string $value

The value of the XML object.

$xml read-only

public string $xml

The XML string of the XML object.

$_document

The raw document object.

private DOMDocument|null $_document

This property holds the raw \DOMDocument object.

$_node

The raw element object.

private DOMNode|null $_node

This property holds the raw \DOMNode object representing an element.

$_tag

Additional data.

private object|null $_tag

This property holds additional data associated with the XML object.

Methods

__construct()

Constructor.

public __construct(DOMNode $node[, DOMDocument|null $dom = null ]) : mixed

Initializes a new instance of the XmlNode class.

Parameters
$node : DOMNode

The node.

$dom : DOMDocument|null = null

The document.

__get()

Getter method.

public __get(string $property) : mixed

This method is called when attempting to access inaccessible properties of the object. It returns the value of the requested property.

Parameters
$property : string

The requested property.

Return values
mixed

The value of the requested property.

__set()

Setter method.

public __set(string $property, mixed $value) : void

This method is called when attempting to set inaccessible properties of the object. It sets the value of the specified property.

Parameters
$property : string

The property to be set.

$value : mixed

The value to be assigned to the property.

Append()

Adds the specified nodes/node to the end.

public Append(mixed $nodes) : void

This method adds the specified nodes or node to the end of the current node.

Parameters
$nodes : mixed

The nodes or node to append.

CreateTextNode()

Creates a text node with the specified content.

public CreateTextNode(mixed $string) : XmlNode

This method creates a text node with the specified content.

Parameters
$string : mixed

The content of the text node.

Return values
XmlNode

The created text node.

Export()

Exports the XML data with a specified document tag.

public Export(string $documentTag) : self

This method exports the XML data with the specified document tag by creating a new XML node with the provided tag and appending the current XML node to it. The method returns a new instance of the XmlNode class representing the exported XML.

Parameters
$documentTag : string

The document tag to use for exporting.

Return values
self

A new instance of the XmlNode class representing the exported XML.

FromObject()

Restores a node from an XmlSerialized object.

public static FromObject(XmlSerialized $xmlSerializedObject[, mixed|null $elementDefinition = null ]) : XmlNode

This method restores a node from the specified XmlSerialized object, optionally using the provided element definition for orientation.

Parameters
$xmlSerializedObject : XmlSerialized

The object to restore from.

$elementDefinition : mixed|null = null

The definition of elements to orient towards.

Return values
XmlNode

The restored XmlNode object.

getElementsByName()

Returns elements with an attribute @name containing the specified name.

public getElementsByName(string $name) : XmlNamedNodeList

This method returns elements with an attribute @name containing the specified name.

Parameters
$name : string

The name of the attribute.

Return values
XmlNamedNodeList

The list of nodes.

Insert()

Adds the specified nodes/node before the $relation node.

public Insert(mixed $nodes, XmlNode $relation) : void

This method adds the specified nodes or node before the specified $relation node.

Parameters
$nodes : mixed

The nodes or node to insert.

$relation : XmlNode

The node before which to insert.

IsChildOf()

Checks if the current node is a child of the specified node.

public IsChildOf(XmlNode $node) : bool

This method checks if the current node is a child of the specified node.

Parameters
$node : XmlNode

The node to check against.

Return values
bool

True if the current node is a child of the specified node, false otherwise.

Item()

Returns an XmlNode object corresponding to the child object with the name $name.

public Item(string $name) : XmlNode|null

This method returns an XmlNode object that corresponds to the child node with the specified $name.

Parameters
$name : string

The name of the child node.

Return values
XmlNode|null

An XmlNode object corresponding to the specified child node, or null if not found.

Items()

Returns an XmlNodeList with the tag name $name.

public Items(string $name) : XmlNodeList

This method returns an XmlNodeList containing all child nodes with the specified tag name.

Parameters
$name : string

The tag name of the child nodes.

Return values
XmlNodeList

An XmlNodeList containing child nodes with the specified tag name.

Load()

Creates an XmlNode object from a string or a file.

public static Load(string $xmlFile[, bool $isFile = true ]) : XmlNode

This method creates an XmlNode object either from a string containing XML data or from an XML file.

Parameters
$xmlFile : string

The XML file path or XML string.

$isFile : bool = true

Indicates whether the provided argument is a file path or a string.

Return values
XmlNode

The created XmlNode object.

LoadHTML()

Creates an XmlNode object from an HTML string or file.

public static LoadHTML(string $htmlFile[, bool $isFile = true ][, string $encoding = 'utf-8' ]) : XmlNode

This method creates an XmlNode object either from a string containing HTML data or from an HTML file.

Parameters
$htmlFile : string

The HTML file path or HTML string for loading.

$isFile : bool = true

Indicates whether the provided argument is a file path or not.

$encoding : string = 'utf-8'

The encoding of the file or string (default: utf-8).

Return values
XmlNode

The created XmlNode object.

LoadHtmlNode()

Creates an XMLHtmlNode from an incomplete document.

public static LoadHtmlNode(string $xmlString[, string $encoding = 'utf-8' ]) : XmlNode

This method creates an XMLHtmlNode object from a string containing partial HTML data.

Parameters
$xmlString : string

The HTML string.

$encoding : string = 'utf-8'

The encoding of the string (default: utf-8).

Return values
XmlNode

The created XMLHtmlNode object.

LoadNode()

Creates an XmlNode from an incomplete document.

public static LoadNode(string $xmlString[, string $encoding = 'utf-8' ]) : XmlNode

This method creates an XmlNode object from a string containing partial XML data.

Parameters
$xmlString : string

The XML string.

$encoding : string = 'utf-8'

The encoding of the string (default: utf-8).

Return values
XmlNode

The created XmlNode object.

Path()

Returns the path based on the query.

public Path(string $query) : string

This method returns the path by querying each parent for data.

Parameters
$query : string

The query to each parent for returning data.

Return values
string

The path determined by the query.

Query()

Executes an XPath query.

public Query(string $query[, bool $returnAsNamedMap = false ][, array<string|int, mixed> $namespaces = [] ]) : XmlNodeList|XmlNamedNodeList

This method executes the specified XPath query and returns the result as either an XmlNodeList or an XmlNamedNodeList.

Parameters
$query : string

The XPath query string.

$returnAsNamedMap : bool = false

Whether to return the result as a named map.

$namespaces : array<string|int, mixed> = []

An array of namespace prefixes and URIs.

Return values
XmlNodeList|XmlNamedNodeList

The result of the XPath query.

Remove()

Removes the current node.

public Remove() : void

This method removes the current node from its parent node.

ReplaceTo()

Replaces the current node with the specified node.

public ReplaceTo(XmlNode $node) : void

This method replaces the current node with the specified node.

Parameters
$node : XmlNode

The node to replace with.

Save()

Saves the XML stored in the object to a file or returns it as a string.

public Save([string $filename = "" ]) : string|null

This method either saves the XML stored in the object to a specified file or returns it as a string if no filename is provided.

Parameters
$filename : string = ""

The path to the file for saving. If not specified, the XML string will be returned.

Return values
string|null

The XML string if $filename is empty, otherwise null.

SaveHTML()

Saves the HTML stored in the object to a file or returns it as a string.

public SaveHTML([string $filename = "" ]) : string|null

This method either saves the HTML stored in the object to a specified file or returns it as a string if no filename is provided.

Parameters
$filename : string = ""

The path to the file for saving. If not specified, the HTML string will be returned.

Return values
string|null

The HTML string if $filename is empty, otherwise null.

ToObject()

Converts the current node and its children into an XmlSerialized object.

public ToObject([array<string|int, mixed> $exclude = [] ][, int|null $levels = null ]) : XmlSerialized|XmlCData|string|null

This method converts the current node and its children into an XmlSerialized object, optionally excluding specified attributes and nodes and limiting the number of levels to be included.

Parameters
$exclude : array<string|int, mixed> = []

An array of attribute and node names to exclude.

$levels : int|null = null

The number of levels to include.

Return values
XmlSerialized|XmlCData|string|null

The converted XmlSerialized object, XmlCData, string, or null.


        
On this page

Search results