PHP array to generate XML Format Data encapsulation class instance _php skill

Source: Internet
Author: User

The examples in this article describe the encapsulated classes of XML-formatted data generated by the PHP array. Share to everyone for your reference, specific 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 XML file from array */class Fwcms_makexml {private static $instance; 
    Private Function __construct () {}/** * Single-piece mode calls this class * * @return single piece mode/public static function getinstance () {
    if (!isset (self::instance)) {self::instance = new fwcms_makexml ();
  return self::instance; /** * Gets the XML string * @param $array an array of XML that can be two-dimensional or multidimensional, with the first element being the XML element name * @param $xslName XSL file name (for example: "http://www. Xxx.com/templates/normal/xslname.xsl ") * @return $XMLString Output XML String */Public function GetXML ($array, $xslName =" ")
    {$XMLString = ' <?xml version= ' 1.0 ' encoding= ' utf-8 '?> '; if ($xslName!= "") $XMLString. = ' <?xml-stylesheet type= ' text/xsl ' href= '. $xslName ' "?
    > ';
    $XMLString. = $this->make ($array);
  return $XMLString; } * * RecursionGenerate 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 no specified then the element named Item} $XMLString. = ' < '. $elementName. '
    '; if (Is_array ($array)) {foreach ($array as $paramKey => $nodeParam) {if (!is_array ($nodeParam)) {//If not A subordinate element, that is the parameter of the element $XMLString. = $paramKey. ' = "'. $nodeParam."
        ';
            }else{if (! $haveRightBracket) {$XMLString. = ' > ';
          $haveRightBracket =true;
        //If it is a subordinate element, append the element $XMLString. = $this->make ($nodeParam);
      }} if (! $haveRightBracket) {$XMLString. = ' > ';
    $haveRightBracket =true; $XMLString. = ' </'. $elementName. '
  >//The element handles the end return $XMLString; /** * Save string to file * @param $fileName filename * @param $XMLString the generated XML string/Public function SavetOfile ($fileName, $XMLString) {if (! $handle =fopen ($fileName, ' W ')) {return FALSE;
    } if (!fwrite ($handle, $XMLString)) {return FALSE;
  return TRUE; /** * Generate XML file directly via array/Public function write ($fileName, $array, $xslName = ') {$XMLString = $this->getxml ($a
    Rray, $xslName);
    $result = $this->savetofile ($fileName, $XMLString);
  return $result;

 }
}

Test Code testxml.php:

<?php require_once ' makexml.php ';
$xml =fwcms_makexml::getinstance ();
$xml =new fwcms_makexml (); $array =array (' elementname ' => ' data ',//xml node name, if not defined, default to item ' Test1 ' =>array (//Do not define this section naming//' Elementna 
      Me ' => ' item1 ', ' id ' =>1, ' title ' => ' Try1 '), ' test2 ' =>array (' elementname ' => ' item2 ',
        ' ID ' =>2,//parameter ' title ' => ' Try2 ',//Parameter ' test2.2 ' =>array (//xml child node, must be placed after all parameters and cannot be followed by arguments
      ' ElementName ' => ' item2.2 ', ' id ' =>2.2, ' title ' => ' try2.2 '), ' Test3 ' =>array ( ' ElementName ' => ' item3 ', ' id ' =>3, ' title ' => ' Try3 '), ' test4 ' =>array (' Elementna Me ' => ' item4 ', ' id ' =>4, ' title ' => ' Try4 '), ' Test5 ' =>array (//Do not define this section named///' element
Name ' => ' item5 ', ' id ' =>5, ' title ' => ' Try5 '); $string = $xml->getxml ($array);//The XML string generated by the array $xml->savetofile (' Test.xml ', $string);//The generated XMLThe string is written to the file $xml->write (' Test2.xml ', $array, ' test2.xsl ');//Get the string generated header ("Content-type:text/xml") directly from the array;

 echo $string;?>

More about PHP Interested readers can view the site topics: "PHP for XML file Operation skills Summary", "PHP Array" Operation Techniques Encyclopedia, "PHP string (String) Usage summary", "PHP error and Exception handling method summary", " Summary of PHP operation and operator usage, "Summary of PHP Network programming skills", "Introduction to PHP Basic Grammar", "PHP Introduction to Object-oriented Programming", "Php+mysql Database Operation Introduction" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

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.