PHP Classes

XML Helper: Extract XML data into an array and vice-versa

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 343 All time: 7,058 This week: 660Up
Version License PHP version Categories
xml-helper 11.1.2MIT/X Consortium ...5.3XML, PHP 5
Description 

Author

This class can be used to extract XML data into an array and vice-versa.

It can parse a given XML document and return a nested array that describes the XML document structure and data.

The class can also do the opposite, i.e. take an array with a document structure and data and generate a XML document from it.

A full PHPUnit test suite is included. You'll need to have PHPUnit installed to use it.

Picture of Richard Williams
  Performance   Level  
Innovation award
Innovation award
Nominee: 1x

 

Recommendations

convert array to xml
I need to convert array to XML

Documentation

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.

  Files folder image Files (14)  
File Role Description
Files folder imagedoc (1 file)
Files folder imagetests (2 files, 1 directory)
Accessible without login Plain text file XmlHelper.class.php Class Class

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:343
This week:0
All time:7,058
This week:660Up