Home > Backend Development > PHP Tutorial > PHP array generates encapsulated class instances of XML format data

PHP array generates encapsulated class instances of XML format data

高洛峰
Release: 2023-03-04 06:24:01
Original
1007 people have browsed it

The example in this article describes the encapsulation class for generating XML format data from PHP arrays. Share it with everyone for your reference, 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.
*/
/*
* 从数组生成XML文件
*/
class Fwcms_MakeXML
{
  private static $instance;
  private function __construct(){}
  /**
   * 单件模式调用本类
   *
   * @return 单件模式
   */
  public static function getInstance() {
    if (!isset(self::instance)) {
      self::instance = new Fwcms_MakeXML();
    }
    return self::instance;
  }
  /**
   * 获取XML字串
   * @param $array 用于生成XML的数组,数组可以是二维或多维的,其中的第一个元素作为XML元素名
   * @param $xslName XSL文件名(如:"http://www.xxx.com/templates/normal/xslname.xsl")
   * @return $XMLString 输出XML字符串
   */
  public function getXML($array,$xslName=""){
    $XMLString = &#39;<?xml version="1.0" encoding="utf-8"?>&#39;;
    if($xslName!="")
      $XMLString.=&#39;<?xml-stylesheet type="text/xsl" href="&#39;.$xslName.&#39;"?>&#39;;
    $XMLString.=$this->make($array);
    return $XMLString;
  }
  /*
   * 递归生成XML字串
   */
  private function make($array)
  {
    $XMLString=&#39;&#39;;
    $haveRightBracket=FALSE;
    if(isset($array[&#39;elementName&#39;])){
      $elementName=array_shift($array);//数组的第一个元素为XML元素名
    }else{
      $elementName=&#39;item&#39;;//如果没有指定则元素名为item
    }
    $XMLString.=&#39;<&#39;.$elementName.&#39; &#39;;
    if(is_array($array)){
    foreach($array as $paramKey=>$nodeParam){
        if(!is_array($nodeParam)){
          //如果不是一个下级元素,那就是元素的参数
          $XMLString.=$paramKey.&#39;="&#39;.$nodeParam.&#39;" &#39;;
        }else{
          if(!$haveRightBracket){
            $XMLString.=&#39;>&#39;;
            $haveRightBracket=TRUE;
          }
          //如果是下级元素,则追加元素
          $XMLString.=$this->make($nodeParam);
        }
      }
    }
    if(!$haveRightBracket){
      $XMLString.=&#39;>&#39;;
      $haveRightBracket=TRUE;
    }
    $XMLString.=&#39;</&#39;.$elementName.&#39;>&#39;;//该元素处理结束
    return $XMLString;
  }
  /**
   * 将字串保存到文件
   * @param $fileName 文件名
   * @param $XMLString 已经生成的XML字串
   */
  public function saveToFile($fileName,$XMLString)
  {
    if(!$handle=fopen($fileName,&#39;w&#39;))
    {
      return FALSE;
    }
    if(!fwrite($handle,$XMLString))
    {
      return FALSE;
    }
    return TRUE;
  }
  /**
   * 直接通过数组生成XML文件
   */
  public function write($fileName,$array,$xslName=&#39;&#39;){
    $XMLString=$this->getXML($array,$xslName);
    $result=$this->saveToFile($fileName,$XMLString);
    return $result;
  }
}
Copy after login

Test code testXML.php:

<?php
require_once &#39;MakeXML.php&#39;;
$xml=Fwcms_MakeXML::getInstance();
//$xml=new Fwcms_MakeXML();
$array=array(
    &#39;elementName&#39;=>&#39;data&#39;,//XML节点名,如不定义则默认为ITEM
    &#39;test1&#39;=>array(
      //不定义本节点名
      //&#39;elementName&#39;=>&#39;item1&#39;,
      &#39;id&#39;=>1,
      &#39;title&#39;=>&#39;try1&#39;
    ),
    &#39;test2&#39;=>array(
      &#39;elementName&#39;=>&#39;item2&#39;,
      &#39;id&#39;=>2,//参数
      &#39;title&#39;=>&#39;try2&#39;,//参数
      &#39;test2.2&#39;=>array(
        //XML子节点,必须放在所有参数之后,后面不可以再跟参数
        &#39;elementName&#39;=>&#39;item2.2&#39;,
        &#39;id&#39;=>2.2,
        &#39;title&#39;=>&#39;try2.2&#39;
      )
    ),
    &#39;test3&#39;=>array(
      &#39;elementName&#39;=>&#39;item3&#39;,
      &#39;id&#39;=>3,
      &#39;title&#39;=>&#39;try3&#39;
    ),
    &#39;test4&#39;=>array(
      &#39;elementName&#39;=>&#39;item4&#39;,
      &#39;id&#39;=>4,
      &#39;title&#39;=>&#39;try4&#39;
    ),
    &#39;test5&#39;=>array(
      //不定义本节点名
      //&#39;elementName&#39;=>&#39;item5&#39;,
      &#39;id&#39;=>5,
      &#39;title&#39;=>&#39;try5&#39;
    )
  );
$string=$xml->getXML($array);//由数组生成XML字串
$xml->saveToFile(&#39;test.xml&#39;,$string);//把生成的XML字串写入到文件
$xml->write(&#39;test2.xml&#39;,$array,&#39;test2.xsl&#39;);//直接从数组中获取字串生成文件
header ("content-type: text/xml");
echo $string;
?>
Copy after login

Hope this article will be helpful to everyone in PHP program design .

For more related articles on encapsulation class examples of generating XML format data from PHP arrays, please pay attention to the PHP Chinese website!


Related labels:
xml
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template