This article mainly introduces the encapsulation class for PHP array generation of XML format data, and analyzes the related operation skills of php for recursive array traversal and XML format data conversion based on the complete example form, for more information about how to use PHP arrays to generate data in XML format, see the following example. We will share this with you for your reference. The details are as follows:
Class library code: MakeXML. php:
'; If ($ response name! = "") $ XMLString. ='
'; $ XMLString. = $ this-> make ($ array); return $ XMLString;}/** Recursively generate an XML string */private function make ($ array) {$ XMLString = ''; $ haveRightBracket = FALSE; if (isset ($ array ['elementname']) {$ elementName = array_shift ($ array ); // The first element of the array is the XML element name} else {$ elementName = 'ITEM'; // if not specified, the element name is item} $ XMLString. = '<'. $ elementName. ''; if (is_array ($ array) {foreach ($ array as $ paramKey => $ nodeParam) {if (! Is_array ($ nodeParam) {// if it is not a lower-level element, it is the element parameter $ XMLString. = $ paramKey. '= "'. $ nodeParam. '"';} else {if (! $ HaveRightBracket) {$ XMLString. = '>'; $ haveRightBracket = TRUE;} // if it is a lower-level element, the element $ XMLString is appended. = $ this-> make ($ nodeParam) ;}} if (! $ HaveRightBracket) {$ XMLString. = '>'; $ haveRightBracket = TRUE;} $ XMLString. ='
'; // Return $ XMLString ;} /*** save the string to the file * @ param $ fileName File Name * @ param $ XML string generated */public function saveToFile ($ fileName, $ XMLString) {if (! $ Handle = fopen ($ fileName, 'w') {return FALSE;} if (! Fwrite ($ handle, $ XMLString) {return FALSE;} return TRUE;}/*** generate an XML file directly through an array */public function write ($ fileName, $ array, $ response name = '') {$ XMLString = $ this-> getXML ($ array, $ response name); $ result = $ this-> saveToFile ($ fileName, $ XMLString ); return $ result ;}}
Test code testXML. php:
'Data', // XML node name, if this parameter is not specified, the default value is ITEM 'test1' => array (// the current node name is not defined. // 'elementname' => 'item1', 'id' => 1, 'title' => 'try1'), 'test2' => array ('elementname' => 'item2', 'id' => 2, // parameter 'title' => 'try2', // parameter 'test2. 2 '=> array (// XML subnode, which must be placed after all parameters, and cannot be followed by the parameter 'elementname' => 'item2. 2', 'id' => 2.2, 'title' => 'try2. 2 '), 'test3' => array ('elementname' => 'item3', 'id' => 3, 'title' => 'try3 '), 'test4' => array ('elementname' => 'item4', 'I D' => 4, 'title' => 'try4 '), 'test5' => array (// This node name is not defined // 'elementname' => 'item5', 'id' => 5, 'title' => 'try5'); $ string = $ xml-> getXML ($ array); // The XML string generated by the array $ xml-> saveToFile ('test. XML', $ string); // write the generated xml string to the file $ XML-> write ('test2. XML', $ array, 'test2. xsl '); // Obtain the header ("content-type: text/xml") of the string generation file from the array directly; echo $ string;?>
I hope this article will help you with PHP programming.
For more articles about encapsulation instances for generating XML format data from PHP arrays, refer to PHP Chinese network!