PHP处理XML

工作中遇到用PHP处理XML的相关东西。刚刚学PHP,代码中有很多值得优化的地方。
主要运用simpleXML处理。写了一个通用化的XML处理代码。


Xmlcreater.php

<?php
/**
*   @author:    Even
*   @version:   1.0.1
*   @date:      2017.5.17
*/


define('LOAD_XMLFILE_ERROR',-1);
define('FILE_IO_ERROR', -2);

define('XML_HEADER','<?xml version="1.0" encoding="utf-8" ?>');


class Xmlcreater{
   

    protected $m_arrayXMLData = null;
    protected $m_errorMessage = null;
    protected $m_objXML          = null;   /*This is only part of the XML input object. */
    protected $m_rootName    = '';
    protected $m_xmlFilePath  = '';
    //protected $m_xmlString    = '';      /*This is only part of the XML output string.*/
    protected $m_xmlFileObj   = null;

    /*There must be a parameter for the time being.*/
    public function __construct(){
   
        $argNum = func_num_args();
        switch($argNum){
            case 0:
                $this->m_xmlFilePath = '';
                break;
            case 1:
                $this->m_xmlFilePath = func_get_arg(0);
                break;
        }

        /*Load XML File.*/
        try{
            if (!file_exists($this->m_xmlFilePath)) {  
                $this->m_xmlFileObj = fopen($this->m_xmlFilePath,'w+');
                fwrite($this->m_xmlFileObj,'<a> </a>');
                fclose($this->m_xmlFileObj);
                $this->m_xmlFileObj = null;
            }
            libxml_disable_entity_loader(false);  
            $this->m_objXML = simplexml_load_file($this->m_xmlFilePath,'SimpleXMLElement', LIBXML_NOCDATA); 
        }
        catch(Exception $e){
            $this->m_errorMessage = LOAD_XMLFILE_ERROR;
        }
    }

    public function __destruct() {
   
        if(null != $this->m_xmlFileObj){
            fclose($this->m_xmlFileObj);
            $this->m_xmlFileObj = null;
        }

    }


    /*Get Data From Database For Create XML File.*/
    public function getData(){
   
        $this->m_arrayXMLData = $this->xmlToArray($this->m_objXML);
        return $this->m_arrayXMLData;
    }

    public function getRootName(){
   
        return $this->m_rootName;
    }

    public function setRootName($strName){
   
        $this->m_rootName = $strName;
    }

    /**
    *   @param: Source Data Array, XML Root Name, The XML Object.
    *   @return String: XML String. 
    *   @summary: 
    *       The #3 Parament Only For Recursive Functions, We Can Use $dataArray And $rootName When We Call This Function.
    *
    */
    public function arrayToXML($dataArray, $rootName, &$xmlObj = null ){
   
        $ptrArray   = null;
        $ptrElement = null;

        /*Create XML Object.*/
        if(null == $xmlObj){
            $tempXml = XML_HEADER.'<'.$rootName.'></'.$rootName.'>';
            $xmlObj  = simplexml_load_string($tempXml);
        }

        if(null != $rootName){
            $dataArray = $dataArray[$rootName];
        }

        /*Add Attributes.*/
        if(array_key_exists('@attributes',$dataArray)) {
            foreach ($dataArray['@attributes'] as $keyAttr => $valueAttr) {
                $xmlObj->addAttribute($keyAttr,$valueAttr);
            }
        }

        /*Add Element.*/
        //CHECKIT: if(is_array($valueEle));
        foreach ($dataArray as $keyEle => $valueEle) {
            if($keyEle != '@attributes'){
                if(is_array($valueEle)){
                    $ptrArray = $valueEle;
                    $ptrElement = $xmlObj->addChild($keyEle);
                }
                else{
                    $ptrElement = $xmlObj->addChild($keyEle,$valueEle);
                } 
                $this->arrayToXML($ptrArray,null,$ptrElement);
            }
        }/*End of foreach.*/

        return $xmlObj->saveXML();
    }   /*End Of Function arrayToXML.*/

    /**
    *   @param: The XML String Or Object.
    *   @return: XML Object.
    *
    */
    private function checkValueType(&$xml){
   
        $valueType = gettype($xml);
        switch($valueType){
            case 'string':
                return simplexml_load_string($xml);
            case 'object':
                return $xml;
            default:
                return null;
        }
    }


    /**
    *   @param: Simple XML Object / XML String.
    *   @return: 
    *       a.An Array Of XML Data.
    *       b.If XML String Is Not A Standard XML, Return Null Or Generate An Exception.
    *   @summary: 
    *       XML Object To Array Value.
    *
    */
    public function xmlToArray($xml){
   
        $xml = $this->checkValueType($xml);
        $this->m_arrayXMLData = json_decode(json_encode($xml),true);
        return $this->m_arrayXMLData;
    }   /*End Of Function xmlToArray.*/


    public function createXmlFile($fileInfo, $xmlString){
   
        $tempRes = null;
        try{
            /*Create File.*/
            $this->m_xmlFileObj = fopen($fileInfo,"w");
            //chmod($this->m_xmlFileObj, 0757);
            /*Write File.*/
            $tempRes = fwrite($this->m_xmlFileObj,$xmlString);
            return $tempRes;
        }
        catch(Exception $e){
            return FILE_IO_ERROR;
        }
    }

    public function getXmlString($simplexmlObj){
   
        return $simplexmlObj->saveXML();
    }

    public function getXmlObject(
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值