PHP Classes

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

Recommend this page to a friend!
  Info   View files Documentation   View files View files (14)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 342 All time: 7,054 This week: 423Up
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  
Name: Richard Williams <contact>
Classes: 2 packages by
Country: United States United States
Age: 78
All time rank: 3418460 in United States United States
Week rank: 344 Up44 in United States United States Up
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  
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

  Files folder image Files  /  doc  
File Role Description
  Accessible without login Plain text file XmlHelper.txt Doc. Description of the class

  Files folder image Files  /  tests  
File Role Description
Files folder imagetestXmlData (10 files)
  Accessible without login Plain text file stand-alone-test.php Example non-phpunit example/test
  Accessible without login Plain text file XmlHelperTest.php Test Unit Tests

  Files folder image Files  /  tests  /  testXmlData  
File Role Description
  Accessible without login Plain text file 55521_cwr.xml Data Test data
  Accessible without login Plain text file 55521_item_only.xml Data Test data
  Accessible without login Plain text file 55521_total_only.xml Data Test data
  Accessible without login Plain text file base_test.xml Data Test data
  Accessible without login Plain text file endicia-status.xml Data Test data
  Accessible without login Plain text file ibm.xml Data Test data
  Accessible without login Plain text file large_array.php Aux. Test data
  Accessible without login Plain text file one_element.php Data Test data
  Accessible without login Plain text file purchase-order-sample.xml Data Test data
  Accessible without login Plain text file xmlbase.xml Data Test data

 Version Control Unique User Downloads Download Rankings  
 0%
Total:342
This week:0
All time:7,054
This week:423Up