php 生成xml文件

时间:2023-01-13 23:39:05

<?php

class Xml
{    
    /*
     *  $aData     要格式化的数组
     *  $path      xml信息要写入的文件路径
     *  $version   xml文件的版本信息
     *  $encoding  xml文件信息的编码形式
     *  返回 : false  数组格式不正确
                true   成功
     */
    private $_strEcode;
    public function array2XmlFile ( $aData , $path , $version='1.0' , $encoding = 'GBK')
    {
        $this->_strEcode = $encoding;
        //判断数组格式是否正确
        $bFlag = $this ->checkArray($aData);
        if(!$bFlag)
        {
            return false;    
        }                        
        $xmlDoc = @new DOMDocument( $version , $encoding );
        $xmlDoc -> formatOutput=true;                       
        foreach($aData as $kData => $vData)
        {    
            //声明根节点 upload
            $objRoot = $xmlDoc->createElement ($kData) ;    
            $objRoot = $xmlDoc->appendChild($objRoot);      
            
            //添加根节点属性 icp
            if(is_array($vData))
            {                
                $xmlDoc = $this-> formatXml($vData , $kData , $xmlDoc , $objRoot);  
            }
        }     
        if($xmlDoc-> save($path))
        { //保存路径
            return true;
        }
        else
        {
            return false ;
        }   
                  
    }
    
    /*
     *
     
    */
    public function array2Xmlstring( $aData , $version='1.0' , $encoding = 'GBK')
    {
        $this->_strEcode = $encoding;
        //判断数组格式是否正确
        $bFlag = $this ->checkArray($aData);
        if(!$bFlag)
        {
            return false;    
        }                        
        $xmlDoc = @new DOMDocument( $version , $encoding );
        $xmlDoc -> formatOutput=true;                       
        foreach($aData as $kData => $vData)
        {    
            //声明根节点 upload
            $objRoot = $xmlDoc->createElement ($kData) ;    
            $objRoot = $xmlDoc->appendChild($objRoot);      
            
            //添加根节点属性 icp
            if(is_array($vData))
            {                
                $xmlDoc = $this-> formatXml($vData , $kData , $xmlDoc , $objRoot);  
            }
        }
        
        return $xmlDoc->saveXML();               
    }
        
    private function checkArray($aData)
    {        
        $bIntFlag = false ;
        $bStrFlag = false ;
        $bResult = true;        
        foreach($aData as $k => $v)
        {  
            $strKType = gettype($k);
            if(strcmp($strKType , 'integer') == 0)
            {
                $bIntFlag = true;
            }
            elseif(strcmp($k , "Attribute") != 0)
            {
                $bStrFlag = true;   
            }                 
        }
        if( $bIntFlag  and $bStrFlag)
        {
            return false;
        }            
        else
        {        
            foreach($aData as $kk => $vv)
            {
                if(is_array($vv))
                {
                    $bResult = $this -> checkArray($vv);
                    if(!$bResult)
                    {
                        break;
                    }
                }
            }            
            return $bResult ;
        }
    }
    
    private function formatXml($aData , $kFront , $objDoc , $objFrnDoc , $objStepDoc= null)
    {         
        $nCount = 0;
        foreach( $aData as $kData => $vData)
        {           
            if(is_string($kData))
            {
                $kData = trim( $kData );
            }            
            if(strcmp ( $kData , "Attribute") == 0)
            {
                foreach($vData as $kAttr => $vAttr)
                {
                    $strAttr = $objDoc->createAttribute($kAttr);   //创建节点属性对象实体  
                    $objAttr = $objFrnDoc->appendChild($strAttr);  //把属性添加到节点info中   
                    $objAttr -> appendChild($objDoc->createTextNode($vAttr));        
                }      
            }                                      
            else
            {  
                if(!is_int(($kData)))
                {  
                    $objCurrDoc = $objDoc -> createElement ($kData);
                    $objCurrDoc = $objFrnDoc->appendChild($objCurrDoc);                                                                    
                }
                else
                {   
                    if($nCount == 0)
                    {
                        $objCurrDoc = $objFrnDoc;
                        $nCount ++ ;
                    }
                    else
                    {                 
                        $objCurrDoc = $objDoc -> createElement ($kFront);
                        $objCurrDoc = $objStepDoc->appendChild($objCurrDoc);
                    }
                }
                                
                if(!is_array($vData) )
                {                    
                    if(strcasecmp($this->_strEcode , 'utf-8') == 0)
                    {
                        $objCurrDoc->appendChild($objDoc->createTextNode($vData));
                    }
                    else
                    {
                        //$objCurrDoc->appendChild($objDoc->createTextNode(mb_convert_encoding ($vData ,"UTF-8", "GB2312")));
                        $objCurrDoc->appendChild($objDoc->createTextNode(mb_convert_encoding ($vData ,"UTF-8", "GBK")));
                    }
                }
                else
                {   
                    $objDoc = $this ->formatXml($vData , $kData , $objDoc  , $objCurrDoc , $objFrnDoc);                           
                }
            }           
        }    
        return $objDoc;
    }
        
    /*
     * $objXml xml格式对象
     * 返回 : 对应格式数组
     */
    public function XmlString2Arr($xml)
    {
        if(is_string($xml))
        {
            $xml = simplexml_load_string($xml);
        }
        $aXml = (array) $xml;        
        foreach($aXml as $k => $v)
        {
            if(is_object($v) || is_array($v))
            {
                $aXml[$k] = $this -> XmlString2Arr($v);
            }            
        }
        return $aXml;
    }
       
    
}