XmlArray
in package
Class XmlArray Represents an XML array
Table of Contents
Properties
- $array : array<string|int, mixed>
- $debug_level : int
- $trim : bool
- holds trim level textual data
Methods
- __construct() : mixed
- Constructor for the xml parser.
- _to_cdata() : string
- Parse out CDATA tags. (htmlspecialchars them...)
- count() : int
- Count the number of occurrences of a path.
- create_xml() : string
- Create an xml file from an XmlArray, the specified path if any.
- exists() : bool
- Check if an element exists.
- fetch() : string
- Get a specified element's value or attribute by path.
- name() : string
- Get the root element's name.
- path() : XmlArray|string|bool
- Get an element, returns a new XmlArray.
- set() : array<string|int, XmlArray>
- Get an array of XmlArray's matching the specified path.
- to_array() : array<string|int, mixed>
- Output the xml in an array form.
- _array() : string|array<string|int, mixed>
- Return an element as an array
- _fetch() : string
- Given an array, return the text from that array. (recursive and privately used.)
- _from_cdata() : string
- Turn the CDATAs back to normal text.
- _parse() : array<string|int, mixed>
- Parse data into an array. (privately used...)
- _path() : string|array<string|int, mixed>
- Get a specific array by path, one level down. (privately used...)
- _xml() : string
- Get a specific element's xml. (privately used...)
Properties
$array
public
array<string|int, mixed>
$array
Holds parsed XML results
$debug_level
public
int
$debug_level
The debugging level
$trim
holds trim level textual data
public
bool
$trim
Holds trim level textual data
Methods
__construct()
Constructor for the xml parser.
public
__construct(string|array<string|int, mixed> $data[, bool $auto_trim = false ][, int $level = null ][, bool $is_clone = false ]) : mixed
Example use: $xml = new XmlArray(file('data.xml'));
Parameters
- $data : string|array<string|int, mixed>
-
The xml data or an array of, unless is_clone is true.
- $auto_trim : bool = false
-
Used to automatically trim textual data.
- $level : int = null
-
The debug level. Specifies whether notices should be generated for missing elements and attributes.
- $is_clone : bool = false
-
default false. If is_clone is true, the XmlArray is cloned from another - used internally only.
_to_cdata()
Parse out CDATA tags. (htmlspecialchars them...)
public
_to_cdata(string $data) : string
Parameters
- $data : string
-
The data with CDATA tags included
Return values
string —The data contained within CDATA tags
count()
Count the number of occurrences of a path.
public
count(string $path) : int
Example use: echo $xml->count('html/head/meta');
Parameters
- $path : string
-
The path to search for.
Return values
int —The number of elements the path matches.
create_xml()
Create an xml file from an XmlArray, the specified path if any.
public
create_xml([string $path = null ]) : string
Example use: echo $this->create_xml();
Parameters
- $path : string = null
-
The path to the element. (optional)
Return values
string —Xml-formatted string.
exists()
Check if an element exists.
public
exists(string $path) : bool
Example use, echo $xml->exists('html/body') ? 'y' : 'n';
Parameters
- $path : string
-
The path to the element to get.
Return values
bool —Whether the specified path exists
fetch()
Get a specified element's value or attribute by path.
public
fetch(string $path[, bool $get_elements = false ]) : string
Children are parsed for text, but only textual data is returned unless get_elements is true. Example use: $data = $xml->fetch('html/head/title');
Parameters
- $path : string
-
The path to the element to fetch
- $get_elements : bool = false
-
Whether to include elements
Return values
string —The value or attribute of the specified element
name()
Get the root element's name.
public
name() : string
Example use: echo $element->name();
Return values
string —The root element's name
path()
Get an element, returns a new XmlArray.
public
path(string $path[, bool $return_full = false ]) : XmlArray|string|bool
It finds any elements that match the path specified. It will always return a set if there is more than one of the element or return_set is true. Example use: $element = $xml->path('html/body');
Parameters
- $path : string
-
The path to the element to get
- $return_full : bool = false
-
Whether to return the full result set
Return values
XmlArray|string|bool —a new XmlArray. False if we can not find an attribute
set()
Get an array of XmlArray's matching the specified path.
public
set(string $path) : array<string|int, XmlArray>
This differs from ->path(path, true) in that instead of an XmlArray of elements, an array of XmlArray's is returned for use with foreach. Example use: foreach ($xml->set('html/body/p') as $p)
Parameters
- $path : string
-
The path to search for.
Return values
array<string|int, XmlArray> —An array of XmlArray objects
to_array()
Output the xml in an array form.
public
to_array([string $path = null ]) : array<string|int, mixed>
Example use: print_r($xml->to_array());
Parameters
- $path : string = null
-
The path to output.
Return values
array<string|int, mixed> —An array of XML data
_array()
Return an element as an array
protected
_array(array<string|int, mixed> $array) : string|array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>
-
An array of data
Return values
string|array<string|int, mixed> —A string with the element's value or an array of element data
_fetch()
Given an array, return the text from that array. (recursive and privately used.)
protected
_fetch(null|array<string|int, mixed>|string $array) : string
Parameters
- $array : null|array<string|int, mixed>|string
-
An array of data
Return values
string —The text from the array
_from_cdata()
Turn the CDATAs back to normal text.
protected
_from_cdata(string $data) : string
Parameters
- $data : string
-
The data with CDATA tags
Return values
string —The transformed data
_parse()
Parse data into an array. (privately used...)
protected
_parse(string $data) : array<string|int, mixed>
Parameters
- $data : string
-
The data to parse
Return values
array<string|int, mixed> —The parsed array
_path()
Get a specific array by path, one level down. (privately used...)
protected
_path(array<string|int, mixed> $array, string $path, null|int $level[, bool $no_error = false ]) : string|array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>
-
An array of data
- $path : string
-
The path
- $level : null|int
-
How far deep into the array we should go
- $no_error : bool = false
-
Whether or not to ignore errors
Return values
string|array<string|int, mixed> —The specified array (or the contents of said array if there's only one result)
_xml()
Get a specific element's xml. (privately used...)
protected
_xml(array<string|int, mixed> $array, null|int $indent) : string
Parameters
- $array : array<string|int, mixed>
-
An array of element data
- $indent : null|int
-
How many levels to indent the elements (null = no indent)
Return values
string —The formatted XML