将多维数组转成XML的工具类

  • 2019-02-13
  • 0
  • 0

在开发过程中遇到需要生成xml文件的需求,找了很多资料都没找到好的方法,于是自己写了一个,大家一起来看一下

首先看一下代码:

<?php
/**
 * Created by PhpStorm.
 * User: shanyuliang
 * Date: 2019/1/17
 * Time: 18:08
 */

namespace App\Service;

class ExportXmlService
{
    /**
     * @var array
     * 属性数组
     */
    private static $attribute = [];

    /**
     * @var array
     * CDATA数组
     */
    private static $cdata = [];

    /**
     * @param $data
     * @param array $attribute
     * @param array $cdata
     * @return mixed
     * 生成方法
     */
    public static function generate($data, $attribute = [], $cdata = [])
    {
        self::$attribute = $attribute;

        self::$cdata = $cdata;

        $dom = new \DOMDocument('1.0', 'utf-8');

        $res =  self::createData($data, $dom, null);

        return $res->saveXML();
    }

    /**
     * @param $data
     * @param $dom
     * @param $parent
     * @return mixed
     * 开始生成
     */
    private static function createData($data, $dom, $parent)
    {
        foreach ($data as $key=> $val){
            $key = trim($key);
            if(is_array($val) && $parent == null){
                $element = $dom->createElement($key);
                $dom->appendchild($element);

                self::createAttribute($key, $dom, $element);

                $dom = self::createData($val, $dom, $element);
            }elseif(is_array($val) && $parent != null) {
                if($num = strrpos($key,"|")){
                    $key = substr($key, 0, $num);
                }
                $element = $dom->createElement($key);
                $parent->appendchild($element);

                self::createAttribute($key, $dom, $element);

                $dom = self::createData($val, $dom, $element);
            }elseif($parent != null){
                $element = $dom->createElement($key);
                $parent->appendchild($element);

                self::createAttribute($key, $dom, $element);

                if(!empty($val) && in_array($key, self::$cdata)){
                    self::createCData($dom, $element, $val);
                }else{
                    $text = $dom->createTextNode($val);
                    $element->appendchild($text);
                }
            }else{
                $element = $dom->createElement($key);
                $dom->appendchild($element);

                self::createAttribute($key, $dom, $element);

                if(!empty($val) && in_array($key, self::$cdata)){
                    self::createCData($dom, $element, $val);
                }else{
                    $text = $dom->createTextNode($val);
                    $element->appendchild($text);
                }
            }
        }

        return $dom;
    }

    /**
     * @param $key
     * @param $dom
     * @param $parent
     * 生成属性
     */
    private static function createAttribute($key, $dom, $parent)
    {
        if(isset(self::$attribute[$key]) && !empty($items = self::$attribute[$key])){
            foreach ($items as $k => $item){
                $attr = $dom->createAttribute($k);
                $parent->appendchild($attr);

                $text = $dom->createTextNode($item);
                $attr->appendChild($text);
            }
        }
    }

    /**
     * @param $dom
     * @param $parent
     * @param $val
     * 添加CDATA
     */
    private static function createCData($dom, $parent, $val)
    {
        $cdata = $dom->createCDATASection($val);
        $parent->appendChild($cdata);
    }

    public static function object2arr($object)
    {
        return json_decode(json_encode($object), true);
    }

}

具体使用:

$xml = [
    'xmlHead'   => [
        'A' => 'aaa',
        'B' => 'bbb',
        'C' => [
            'Aa' => '111',
            'Ab' => '222'
        ]
    ]
];

$attribute = [
    'xmlHead' => [
        'xmlns' => 'http://www.shanyuliang.com',
        'date'  => '2019-1-2'
    ]
];

$res = ExportXmlService::generate($xml, $attribute); print_r($res);

输出结果为:

注:如果一个项有多个元素,请用’|’分割键名,例如:

$xml = [
    'xmlHead'   => [
        'A' => 'aaa',
        'B' => 'bbb',
        'C' => [
            'Aa' => '111',
            'Ab' => '222'
        ],
        'D|1'   => [
            'D1'    => 'd1'
        ],
        'D|2'   => [
            'D2'    => 'd2'
        ]
    ]
];

生成:

 

在使用过程中遇到什么问题可以发表讨论或者是有改进的方法亦可进行讨论。

评论

还没有任何评论,你来说两句吧