PHP array to generate an encapsulation instance of XML format data

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.