\XilyXml

The Xml class is being used to work with XML structures.

It can be used to parse an XML file/string and to work dynamically with this information or to manipulate the XML structure.

Summary

Methods
Properties
Constants
__construct()
__destruct()
__toString()
build()
create()
returnNode()
parse()
createNode()
getNodeById()
getNodesByPath()
getNodeByPath()
buildFilter()
trace()
root()
parent()
child()
children()
cdata()
content()
dump()
index()
id()
tag()
value()
attribute()
attributes()
copy()
setIndex()
setAttribute()
setValue()
setTag()
setRoot()
setParent()
setCdata()
addCdata()
addChild()
addChildren()
addContent()
hasValue()
hasChildren()
countChildren()
hasCdata()
countCdata()
hasContent()
hasAttributes()
countAttributes()
hasAttribute()
hasParent()
isTrue()
isFalse()
isNode()
terminate()
removeChild()
removeCdata()
removeChildren()
removeContent()
removeAttribute()
reindex()
insertAfter()
insertBefore()
toString()
toTag()
toJSON()
toTree()
initValue()
initRequest()
equationBuild()
equationCheck()
strStripQuotes()
strStripBrackets()
strStripString()
strStripWhitespaces()
strStripInline()
strShorten()
strToFloat()
htmlDecode()
htmlEncode()
xmlChars()
createUID()
fileFormatDir()
fileRead()
fileReadByLine()
fileWrite()
fileRemoveDir()
fileCopyDir()
fileListDir()
fileGetExtension()
fileRename()
probe()
$strTag
$intIndex
$strValue
$arrAttributes
$arrChildren
$arrCdata
$arrContent
$objRoot
$objParent
$xmlResult
$OPEN_TAGS
No constants found
No protected methods found
No protected properties found
N/A
startTagCallback()
endTagCallback()
cdataCallback()
checkProperty()
getContentTarget()
getContentIndex()
addContentEntry()
terminateChildren()
removeContentEntry()
No private properties found
N/A

Properties

$strTag

$strTag : string

Type

string — The tag type (String).

$intIndex

$intIndex : string

Type

string — The number of the node within the current list (Integer).

$strValue

$strValue : string

Type

string — The value of the node.

$arrAttributes

$arrAttributes : array

Type

array — The array containing the node's attributes.

$arrChildren

$arrChildren : array

Type

array — The array containing the node's children.

$arrCdata

$arrCdata : array

Type

array — The array containing the node's CDATA nodes.

$arrContent

$arrContent : array

Type

array — The array containing the structure of CDATA and XML nodes.

$objRoot

$objRoot : \Xily\Xml

Type

\Xily\Xml — Pointer to the root object.

$objParent

$objParent : \Xily\Xml

Type

\Xily\Xml — Pointer to the parent node.

$xmlResult

$xmlResult : \Xily\Xml

Type

\Xily\Xml — Result container for the parse function.

$OPEN_TAGS

$OPEN_TAGS : Array

Type

Array — List of tags that should be displayed as open, even if the have no value

Methods

__construct()

__construct(string $strTag, string|null $strValue, array $arrAttributes, \Xily\Xml|\Xily\Bean $objParent)

Constructor

Parameters

string $strTag

The tag name

string|null $strValue

The CDATA value

array $arrAttributes

Associative array with attributes

\Xily\Xml|\Xily\Bean $objParent

The parent element

__destruct()

__destruct()

Remove all children on destruction

__toString()

__toString() : string

Converts an XML node to a string

Returns

string

build()

build()

The build() function initializes the node and its children Nothing to do here besides parsing the sub structure of the node.

Deviations of Xml, e.g. Beans, may have different build functions.

create()

create(string $strXML, bool $bolLoadFile) : \Xily\Xml

Loads the XML data from a string or file

Parameters

string $strXML

Name of the XML file or XML string

bool $bolLoadFile

Load from file or parse locally

Returns

\Xily\Xml

returnNode()

returnNode(string $strXML, \Xily\Xml|\Xily\Bean $xmlMom, bool $bolLoadFile) : \Xily\Xml|\Xily\Bean

Initialize the parser by loading a file or an XML string

Parameters

string $strXML

Name of the XML file or XML string

\Xily\Xml|\Xily\Bean $xmlMom

Parent node

bool $bolLoadFile

Load from file or parse locally

Returns

\Xily\Xml|\Xily\Bean

parse()

parse(string $strXML) : \Xily\Xml|bool

Parses an XML string

Parameters

string $strXML

Returns

\Xily\Xml|bool

createNode()

createNode(string $strTag, array $arrAttributes, \Xily\Xml $xmlParent) : \Xily\Xml

Creates a new XML node

(Wrapper function to simplify Bean creation)

Parameters

string $strTag

The tag name

array $arrAttributes

List of attributes

\Xily\Xml $xmlParent

Parent node

Returns

\Xily\Xml

getNodeById()

getNodeById(string $strNodeId) : \Xily\Xml

Returns the first node found with the specified ID

Parameters

string $strNodeId

[id]

Returns

\Xily\Xml

getNodesByPath()

getNodesByPath(string|array $mxtPath) : array

Returns an array of nodes or attributes

Parameters

string|array $mxtPath

[path]

Returns

array

getNodeByPath()

getNodeByPath(string $strNodePath) : \Xily\Xml|false

Will return the fist matching node to be found.

Parameters

string $strNodePath

[path]

Returns

\Xily\Xml|false

buildFilter()

buildFilter(array|string $mxtNode) : array|bool

This is an assisting method for getNodesByPath, in order to shape a path link to be used by the children() method Anatomy (example): node(@attribute == 'condition', id == 'myid') Returns an array containing the filter attribute as key and filter + value as value, e.g. $arrFilter = array('tag' => 'node', 'id' => 'myid', 'attributes' => array('attribute' => 'condition'));

Parameters

array|string $mxtNode

The current node including filter (array format: [{tag including filter}, {tag}, {filter}]

Returns

array|bool

trace()

trace(string $strPath) : string

Trace a single node attribute or property.

Works like getNodeByPath, but it will not return a whole object, but will only evaluate the node's property, e.g. node.subnode.@value

Parameters

string $strPath

[path]

Returns

string

root()

root() : \Xily\Xml

Returns the root object

Returns

\Xily\Xml

parent()

parent() : \Xily\Xml

Returns the parent of the current node If the node has no parent / is root, the function returns false

Returns

\Xily\Xml

child()

child(int|string $mxtID) : \Xily\Xml

Return the specified child of the node.

If no index specified, it will return the first node

Parameters

int|string $mxtID

The child index (int) or tag type (string) [index]

Returns

\Xily\Xml

children()

children(string|array $mxtTag, int|array $mxtIndex, string|array $mxtValue, array $arrAttributes, array $arrPath) : array

Returns the node's children

Parameters

string|array $mxtTag

The node's tag (single value or filter array [value, operator]) [n:tag]

int|array $mxtIndex

The node's index (single value or filter array [value, operator]) [n:index]

string|array $mxtValue

The node's value (single value or filter array [value, operator]) [n:value]

array $arrAttributes

The node's attribute (filter array [[attribute, value, operator], ...]) [a:attributes]

array $arrPath

Checks, if a certain sub-path applies to the node (filter array [[path, value, operator], ...]) [a:path]

Returns

array —

Array of XML nodes

cdata()

cdata(integer $intIndex) : array|string

Returns the CDATA entries of the tag

Parameters

integer $intIndex

The index of the CDATA element (Optional) [n:index]

Returns

array|string

content()

content(integer $intIndex) : array|string|\Xily\Xml

Returns the Content (Children and CDATA) of the Tag

Parameters

integer $intIndex

The index of the content element (Optional) [n:index]

Returns

array|string|\Xily\Xml

dump()

dump() : string

Returns a dump of the node's content.

Returns

string

index()

index() : int

Identifies the zero-indexed position of this XML object within the context of its parent.

Returns

int

id()

id() : string

Returns the node's ID

Returns

string

tag()

tag() : string

Returns the node's tag

Returns

string

value()

value() : string

Returns the node's ID, if any

Returns

string

attribute()

attribute(string $strKey) : string

Returns the value of an attribute

Parameters

string $strKey

[key]

Returns

string —

Attibute value

attributes()

attributes() : array

Returns an associative array containing the node's attributes

Returns

array

copy()

copy() : object

Returns a copy of the given XML object.

Returns

object

setIndex()

setIndex(string $intValue)

Sets the index of the node

Parameters

string $intValue

[value]

setAttribute()

setAttribute(string $strAttribute, string $strValue)

Sets an attribute

Parameters

string $strAttribute

[key]

string $strValue

[value]

setValue()

setValue(string $strValue)

Sets the value of the node

Parameters

string $strValue

[n:value]

setTag()

setTag(string $strTag)

Changes the tag of the node

Parameters

string $strTag

[value]

setRoot()

setRoot(object $xmlRoot, bool $bolPersistent)

Sets the root object for a node or its children

Parameters

object $xmlRoot

The new root object

bool $bolPersistent

If $bolPersistent=true, the object passes the new parent object to all ancestors

setParent()

setParent(object $xmlParent)

Sets the parent object for a node or its children

Parameters

object $xmlParent

The new parent object

setCdata()

setCdata(integer $intIndex, string $strValue)

Changes the content of a CDATA element

Parameters

integer $intIndex

Index of the CDATA element [index]

string $strValue

New Value of the CDATA element [value]

addCdata()

addCdata(string $strValue, int $intIndex, int $intContentIndex)

Adds a new CDATA element

Parameters

string $strValue

New Value of the new CDATA element

int $intIndex

The desired position of the new CDATA element (overrides $intContentIndex!)

int $intContentIndex

The desired position within the content stream

addChild()

addChild(object $xmlNode, int $intIndex, int $intContentIndex) : \Xily\Xml

Adds a new child to the node

This adds a new node at the end of the XML and content array: $xml->addChild(new Xml('div', 'hello world'));

This adds a new node at a specific position within the XML structure (index 3) igonring its position within the content stream: $xml->addChild(new Xml('div', 'hello world'), 3);

This adds a new node at a specific position within the content stream, automatically positioning it within the XML array. $xml->addChild(new Xml('div', 'hello world'), null, 2);

Parameters

object $xmlNode

[node]

int $intIndex

The desired position of the new child node (overrides $intContentIndex!) [n:index]

int $intContentIndex

The desired position within the content stream [n:content_index]

Returns

\Xily\Xml

addChildren()

addChildren(array $arrChildren, int $intIndex) : bool

Adds serveral children

Parameters

array $arrChildren

Array containing the XML elements [children]

int $intIndex

Target index of the child nodes [n:index]

Returns

bool

addContent()

addContent(\Xily\Xml|string $mxtNode, int $intIndex)

Adds a content element (either CDATA or an XML node)

Parameters

\Xily\Xml|string $mxtNode

The content node

int $intIndex

The sequence index

hasValue()

hasValue() : bool

Checks, if the node has a value

Returns

bool

hasChildren()

hasChildren() : bool

Checks, if the node has children

Returns

bool

countChildren()

countChildren() : int

Counts the node's children

Returns

int

hasCdata()

hasCdata() : bool

Checks, if the node has any CDATA tags

Returns

bool

countCdata()

countCdata() : int

Counts the node's CDATA tags

Returns

int

hasContent()

hasContent() : bool

Checks, if the node has any content

Returns

bool

hasAttributes()

hasAttributes() : bool

Checks, if the node has attributes

Returns

bool

countAttributes()

countAttributes() : int

Counts the node's attributes

Returns

int

hasAttribute()

hasAttribute(string|array $mxtAttributes, string $mode) : bool

Checks, if the node has a certain attriubte You can check for one single attribute or you can apply a whole attribute list (represented by an array).

Checking modes are - "normal": Checks, if the node has the specified attributes - "strict": Checks, if the node has exactly (only) the specified attributes - "count" : Counts the number of deviations;

Parameters

string|array $mxtAttributes

[key]

string $mode

[n:mode]

Returns

bool

hasParent()

hasParent() : bool

Checks whether the node has a parent or not

Returns

bool

isTrue()

isTrue(string $strAttribute) : bool

Checks, if a a certain attribute is true

Parameters

string $strAttribute

[key]

Returns

bool

isFalse()

isFalse(string $strAttribute) : bool

Checks, if a a certain attribute is false

Parameters

string $strAttribute

[key]

Returns

bool

isNode()

isNode(string $strTag, int $intIndex, string $strValue, array $arrFilter) : bool

Identifies if a node corresponds to certain properties

Parameters

string $strTag
int $intIndex
string $strValue
array $arrFilter

Returns

bool

terminate()

terminate()

Terminates the node

removeChild()

removeChild(\Xily\Xml|string|int $mxtChild) : bool

Removes a specified child node The child node can either be defined by a path, an index or an XML object

Parameters

\Xily\Xml|string|int $mxtChild

[child]

Returns

bool

removeCdata()

removeCdata(int $intIndex) : bool

Removes a CDATA element

Parameters

int $intIndex

Index of the CDATA element. If NULL all CDATA element will be removed. [n:index]

Returns

bool

removeChildren()

removeChildren(array|string $mxtChildren) : bool

Removes multiple children from a node If no identifier is specified, the function will remove all child nodes Additionally a XML path or array of XML objects can be used as identifier

Parameters

array|string $mxtChildren

[n:children]

Returns

bool

removeContent()

removeContent(int $intIndex) : bool

Removes a content element (value, CDATA or XML)

Parameters

int $intIndex

Content index. If NULL, all content elements will be removed [index]

Returns

bool

removeAttribute()

removeAttribute(string $strAttriubte)

Removes an attribute

Parameters

string $strAttriubte

[key]

reindex()

reindex()

Re-Builds the indexes of all sub-nodes

insertAfter()

insertAfter(object $xmlNode) : bool

Inserts a new node AFTER the current node (on the same level).

The function will reindex the current branch after the inserting operation

Parameters

object $xmlNode

[node]

Returns

bool

insertBefore()

insertBefore(object $xmlNode) : bool

Inserts a new node BEFORE the current node (on the same level) The function will reindex the current branch after the inserting operation

Parameters

object $xmlNode

[node]

Returns

bool

toString()

toString(int $intStructure, bool $intEncode, int $numLevel) : string

Returns the XML code of the tree as string

Parameters

int $intStructure

0: The XML string will be forced into one single line; 1: The XML string is created without additional tabs for content; 2: Insert additional tabs to pretty up the structure

bool $intEncode

0: No enconding of the tag's content; 1: encode special XML-chars (default); 2: add CDATA tags;

int $numLevel

Number of tabs before the current tag

Returns

string

toTag()

toTag(bool $bolEncode) : string

Returns the single XML tag as string

Parameters

bool $bolEncode

If unset, the function will add CDATA-tags for all tag values rather than encoding the XML special chars.

Returns

string

toJSON()

toJSON(bool $bolEncode) : string

Converts the XML tree to a JSON string

Parameters

bool $bolEncode

Return encoded string

Returns

string

toTree()

toTree(array $arrSpaces, string $strSpacer, int $intContentIndex, int $intElemIndex, string $strCdata) : string

Displays the structure of the XML data as plain text tree

Parameters

array $arrSpaces

The collection of connector spaces

string $strSpacer

The indentation character (default: tab)

int $intContentIndex

The node index

int $intElemIndex

The element index

string $strCdata

The node's CDATA value

Returns

string

initValue()

initValue(int|float|array|bool|object|string $mxtValue, string $strType, int|float|array|bool|object|string $mxtDefault) : int|float|array|bool|object|string

Initialize a variable value

Parameters

int|float|array|bool|object|string $mxtValue

Value to initialize

string $strType

Variable type

int|float|array|bool|object|string $mxtDefault

Default value

Returns

int|float|array|bool|object|string —

Initialized value

initRequest()

initRequest(string $strKey, mixed $mxtDefault)

Initializes a request variable

Parameters

string $strKey

Name of the variable

mixed $mxtDefault

Default value

equationBuild()

equationBuild(string $strFilter, bool $bolList) : array

Creates an array containing the tree parameters attribute, operator and value to express an equation. The method also aims to simplify the operator by narrowing them down to <, >, =, and !

Parameters

string $strFilter

The filter string, e.g "x >= 10"

bool $bolList

Returns a list of all equations

Returns

array —

['attribute', 'operator', 'value']

equationCheck()

equationCheck(string $strValue1, string $strValue2, string $strOperator) : bool

Checks, if an equation is correct

Parameters

string $strValue1

First parameter

string $strValue2

Second parameter

string $strOperator

Equation operator (==, !=, <, >, <=, >=)

Returns

bool

strStripQuotes()

strStripQuotes(string $strQuote) : string

Strips the quote symbols form a string

Parameters

string $strQuote

Returns

string

strStripBrackets()

strStripBrackets(string $strQuote) : string

Strips the brackets from the beginning and end of a string

Parameters

string $strQuote

Returns

string

strStripString()

strStripString(string $strText, string $strOpen, string $strClose, bool $bolStrict) : string

Strips defined chars from the beginning and the end of a string

Parameters

string $strText
string $strOpen
string $strClose
bool $bolStrict

Only strip symmetrical strings

Returns

string

strStripWhitespaces()

strStripWhitespaces(string $strText) : string

Strips all whitespaces from the beginning and end of a string

Parameters

string $strText

Returns

string

strStripInline()

strStripInline(string $strText, bool $bolLineBreaks) : string

Reduces a paragraph to one single line, removing all linebreaks, tabs and blank spaces

Parameters

string $strText
bool $bolLineBreaks

If set, also linebreaks are removed (otherwise only tabs and blank spaces)

Returns

string

strShorten()

strShorten(string $strText, int $numLength, bool $bolDots, bool $bolWords) : string

Shortens a string to a specified number of chars.

Parameters

string $strText
int $numLength
bool $bolDots

Puts three dots [...] at the end of the shortened string.

bool $bolWords

Respect single words when shortening a text.

Returns

string

strToFloat()

strToFloat(string $strNumber, bool $bolNegative) : float

Converts a formated string into a float

Parameters

string $strNumber

The formated number, e.g. "1,293.00 €"

bool $bolNegative

Support negative numbers

Returns

float

htmlDecode()

htmlDecode(string $strHTML, bool $bolBreak) : string

Converts a HTML string into a plain text string, decoding existing HTML special chars

Parameters

string $strHTML
bool $bolBreak

Also replace
linebreaks in regular linebreaks "\n"

Returns

string

htmlEncode()

htmlEncode(string $strText, string $bolBreak) : \Xily\unknown

Converts a regular string into HTML code, encoding HTML special chars.

Parameters

string $strText
string $bolBreak

If linebreak is true, the function will add
linebreaks

Returns

\Xily\unknown

xmlChars()

xmlChars(string $strRaw) : string

Encodes special XML characters

Parameters

string $strRaw

Returns

string

createUID()

createUID() : string

Creates a unique identifier

Returns

string

fileFormatDir()

fileFormatDir(string $strDirectory, string $strSlash) : string

Makes sure the directory ends with a slash

Parameters

string $strDirectory

The directory

string $strSlash

The slash format (default is DIRECTORY_SEPARATOR)

Returns

string

fileRead()

fileRead(string $strFile) : string|bool

Reads the contents of a file to a string

Parameters

string $strFile

The filename

Returns

string|bool

fileReadByLine()

fileReadByLine(string $strFile, \Xily\stirng $strFunction) : string

Reads the contents of a file to a string line by line and optionally calls a function for each line

Parameters

string $strFile

The filename

\Xily\stirng $strFunction

Name of the function to be called for each line

Returns

string —

The file contents

fileWrite()

fileWrite(string $strFile, string $strText, bool $bolModeAdd) : bool

Writes a string to a file

Parameters

string $strFile

The filename

string $strText

String to write

bool $bolModeAdd

Appends the string at the end of the file

Returns

bool

fileRemoveDir()

fileRemoveDir(string $strDirectory, bool $bolEmpty) : bool

Recursively removes a directory and the files and subdirectories contained in it

Parameters

string $strDirectory
bool $bolEmpty

Declaration of the directory to be empty

Returns

bool

fileCopyDir()

fileCopyDir(string $strDirectory, string $strDestination, bool $bolCreate) : bool

Recursively copies a directory and the files and subdirectories contained in it

Parameters

string $strDirectory
string $strDestination
bool $bolCreate

This option defines, if the whole directory should be copied or only its contents

Returns

bool

fileListDir()

fileListDir(string $strDirName, string $strFiletype) : array

Returns an array with the contents of a directory

Parameters

string $strDirName

The directory name

string $strFiletype

Filter for certain file extensions

Returns

array

fileGetExtension()

fileGetExtension(string $strFilename) : string

Returns the file extension

Parameters

string $strFilename

Filename

Returns

string

fileRename()

fileRename(string $strOldFileName, string $strNewFileName) : bool

Renames a file

Parameters

string $strOldFileName
string $strNewFileName

Returns

bool

probe()

probe(string $strSource, string $strMessage, int $intLevel) : bool

Echos a message, if modeDebug is active. This function can be very useful to place status messages in your functions, to gain some insights of what your program is doing.

Parameters

string $strSource

The function's name where the probe is placed

string $strMessage

The debug message

int $intLevel

Priority/Level of the message

Returns

bool —

Returns, whether the probe was echoed

startTagCallback()

startTagCallback(resource $objParser, string $strTag, array $arrAttributes) : void

Callback for start tags

Parameters

resource $objParser

Parser resource

string $strTag

Tag name

array $arrAttributes

List of attributes

endTagCallback()

endTagCallback(resource $objParser, string $strTag) : void

Callback for end tags

Parameters

resource $objParser

Parser resource

string $strTag

Tag name

cdataCallback()

cdataCallback(resource $objParser, string $strCdata) : void

Callback for CDATA

Parameters

resource $objParser

Parser resource

string $strCdata

CDATA string

checkProperty()

checkProperty(string $strValue, array|string $mxtFilter)

Checks a single attribute (Utility function for Xml::chilren)

Parameters

string $strValue

the value to check

array|string $mxtFilter

Either the value to compare to or an array with an additional operator [value, operator]

getContentTarget()

getContentTarget(int $intIndex, int $intType) : int

Returns a valid XML/CDATA index for a desired content index

Parameters

int $intIndex
int $intType

Returns

int

getContentIndex()

getContentIndex(int $intTarget, int $intType) : int

Returns the content index of a specified XML/CDATA index

Parameters

int $intTarget
int $intType

Returns

int

addContentEntry()

addContentEntry(int $intIndex, int $intType, int $intTarget) : void

Inserts a new element in the content stream

Parameters

int $intIndex

Index of the content element

int $intType

Type of the content element

int $intTarget

Target index within the content stream

terminateChildren()

terminateChildren(object $xmlNode) : bool

Supporting function for removeChild and removeChildren Kills either all or a specified child node

Parameters

object $xmlNode

Returns

bool

removeContentEntry()

removeContentEntry(int $intIndex, int $intType) : bool

Assisting function to update the content array after removing CDATA or XML

Parameters

int $intIndex

Index of the XML or CDATA tag

int $intType

Content type (XML = 1; CDATA = 0)

Returns

bool