PHP array to generate encapsulation class instances of XML format data, array xml

Source: Internet
Author: User

PHP array to generate encapsulation class instances of XML format data, array xml

This example describes the encapsulation class for generating XML format data from PHP arrays. We will share this with you for your reference. The details are as follows:

Class library code: MakeXML. php:

<? Php/*** MakeXML *** @ author Lin Jiong (slime09@gmail.com) * @ version v1.0 * @ license Copyright (c) 2009 Lin Jiong (www.cn09.com) * The LGPL (http://www.gnu.org/licenses/lgpl.html) licenses. * // ** generate an XML file from the array */class Fwcms_MakeXML {private static $ instance; private function _ construct () {}/*** call this class in single-piece mode ** @ return single-piece mode */public static function getInstance () {if (! Isset (self: instance) {self: instance = new Fwcms_MakeXML ();} return self: instance ;} /*** get the XML string * @ param $ array used to generate an XML array. The array can be two-dimensional or multi-dimensional, the first element is used as the XML element name * @ param $ pipeline name XSL file name (for example, "http://www.xxx.com/templates/normal/xslname.xsl") * @ return $ XMLString output XML string */public function getXML ($ array, $ pipeline name = "") {$ XMLString = '<? Xml version = "1.0" encoding = "UTF-8"?> '; If ($ response name! = "") $ XMLString. = '<? Xml-stylesheet type = "text/xsl" href = "'. $ your name.'"?> '; $ 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. = '</'. $ elementName. '>'; // 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:

<? Phprequire_once 'makexml. php '; $ xml = Fwcms_MakeXML: getInstance (); // $ xml = new Fwcms_MakeXML (); $ array = array ('elementname' => 'data ', // XML node name. If not defined, the default value is ITEM 'test1' => array (// This 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' => 'try 2.2 '), 'test3' => array ('elementname' => 'item3', 'id' => 3, 'title' => 'try3 '), 'test4' => array ('elementname' => 'item4', 'id' => 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-ty Pe: text/xml "); echo $ string;?>

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.