The XmlHelper class provides useful utility functions for XML manipulation.
-------------------------------------
arrayResult = xmlToArray(string $xml)
Given an XML string this function will return an equivalent array. For example given this XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<partb index='33' type='integer'>contentb</partb>
<partc>contentc</partc>
<partc>contentc-2</partc>
</root>
will produce this array
Array
(
[ROOT] => Array
(
[PARTB] => Array
(
[ATTRIBUTES] => Array
(
[INDEX] => 33
[TYPE] => integer
)
[VALUE] => contentb
)
[PARTC] => Array
(
[0] => Array
(
[VALUE] => contentc
)
[1] => Array
(
[VALUE] => contentc-2
)
)
)
)
As you can see any element values that are not child elements are returned in a 'VALUE' array element. Element attributes are returned in an array 'ATTRIBUTES'.
You can turn off the return of attributes with setNoAttributes(true|false). NOTE that this will set scalar values instead of setting them as arrays. So with 'true' the returned array will look like this:
Array
(
[ROOT] => Array
(
[PARTB] => contentb
[PARTC] => Array
(
[0] => contentc
[1] => contentc-2
)
)
)
Any elements that occur multiple times are put in subarrays as shown in the 'PARTC' array above.
Array names are uppercased (folded) by default. This is the default behavior of PHP DOM. This can be changed by calling setCaseFolding(true|false).
White space in values and attributes are preserved unless you call setTrimText(true).
-----------------------------------
stringXml = arrayToXml(array $data)
Given an array this function will produce a valid XML string representation. For example given this array
Array
(
[ROOT] => Array
(
[PARTB] => contentb
[PARTC] => Array
(
[0] => contentc
[1] => contentc-2
)
)
)
will produce this XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<ROOT>
<PARTB>contentb</PARTB>
<PARTC>contentc</PARTC>
<PARTC>contentc-2</PARTC>
</ROOT>
If a data array that contains the "VALUE" arrays is submitted the output will not have "VALUE" XML elements but normal XML values.
|