1. 云栖社区>
  2. PHP教程>
  3. 正文

array2xml和xml2array,xml与array的互相转计划

作者:用户 来源:互联网 时间:2017-12-01 11:25:58

xml计划

array2xml和xml2array,xml与array的互相转计划 - 摘要: 本文讲的是array2xml和xml2array,xml与array的互相转计划, php在做后台服务器的时候,经常会遇到这种情况,需要解析来自前台的xml文件,并将数据以xml格式返回,在这种情况下,xml与php中关联数组的转化是非常频繁的事情。比如flex和其他客户端程序与服务器的交互,经常会使用这种方法。下面是我

php在做后台服务器的时候,经常会遇到这种情况,需要解析来自前台的xml文件,并将数据以xml格式返回,在这种情况下,xml与php中关联数组的转化是非常频繁的事情。比如flex和其他客户端程序与服务器的交互,经常会使用这种方法。下面是我归纳的两个方法,大大简化了xml与数组相互转化的工作量。
[php]
/**
     *
     * 将简单数组转化为简单的xml
     * @param string $data  要进行转化的数组
     * @param string $tag   要使用的标签
     * @example
     * $arr = array(
        'rtxAccount'=>'aaron','ipAddr'=>'192.168.0.12',
        'conferenceList'=>array('conference'=>
                            array(
                                array('conferenceId'=>1212,'conferenceTitle'=>'quanshi 444','smeAccount'=>'[email protected]'),
                                array('conferenceId'=>454,'conferenceTitle'=>'quanshi meetting','smeAccount'=>'[email protected]'),
                                array('conferenceId'=>6767,'conferenceTitle'=>'quanshi meetting','smeAccount'=>'[email protected]'),
                                array('conferenceId'=>232323,'conferenceTitle'=>'quanshi uuu','smeAccount'=>'[email protected]'),
                                array('conferenceId'=>8989,'conferenceTitle'=>'quanshi meetting','smeAccount'=>'[email protected]'),
                                array('conferenceId'=>1234343212,'conferenceTitle'=>'quanshi meetting','smeAccount'=>'[email protected]')
                                )
                            )
        
                                
        );
        转化为:
        <rtxAccount>aaron</rtxAccount>
        <ipAddr>192.168.0.12</ipAddr>
        <conferenceList>
            <conference>
                <conferenceId>1212</conferenceId>
                <conferenceTitle>quanshi 444</conferenceTitle>
                <smeAccount>[email protected]</smeAccount>
            </conference>
            <conference>
                <conferenceId>454</conferenceId>
                <conferenceTitle>quanshi meetting</conferenceTitle>
                <smeAccount>[email protected]</smeAccount>
            </conference>
            <conference>
                <conferenceId>6767</conferenceId>
                <conferenceTitle>quanshi meetting</conferenceTitle>
                <smeAccount>[email protected]</smeAccount>
            </conference>
            <conference>
                <conferenceId>232323</conferenceId>
                <conferenceTitle>quanshi uuu</conferenceTitle>
                <smeAccount>[email protected]</smeAccount>
            </conference>
            <conference>
                <conferenceId>8989</conferenceId>
                <conferenceTitle>quanshi meetting</conferenceTitle>
                <smeAccount>[email protected]</smeAccount>
            </conference>
            <conference>
                <conferenceId>1234343212</conferenceId>
                <conferenceTitle>quanshi meetting</conferenceTitle>
                <smeAccount>[email protected]</smeAccount>
            </conference>
        </conferenceList>
     */ 
    function array2xml($data,$tag = '') 
    { 
        $xml = ''; 
         
        foreach($data as $key => $value) 
        { 
            if(is_numeric($key)) 
            { 
                if(is_array($value)) 
                { 
                    $xml .= "<$tag>"; 
                    $xml .= array2xml($value); 
                    $xml .="</$tag>"; 
                } 
                else 
                { 
                    $xml .= "<$tag>$value</$tag>"; 
                }     
            } 
            else 
            { 
                if(is_array($value)) 
                { 
                    $keys = array_keys($value); 
                    if(is_numeric($keys[0])) 
                    { 
                        $xml .=array2xml($value,$key); 
                    } 
                    else 
                    { 
                        $xml .= "<$key>"; 
                        $xml .=array2xml($value); 
                        $xml .= "</$key>"; 
                    } 
                     
                } 
                else 
                { 
                    $xml .= "<$key>$value</$key>"; 
                } 
            } 
        } 
        return $xml; 
    }              

xml2array

[php]
/**
     * 
     * 将简单的xml转化成关联数组
     * @param string $xmlString  xml字符串
     * @example
     * <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RTXConferenceReqDTO>
    <conferenceTitle>IT交流会</conferenceTitle>
    <startTime>2011-12-19 12:00:00</startTime>
    <rtxAccount>andy1111111</rtxAccount>
    <ipAddr>192.168.1.56</ipAddr>
    <duration>120</duration>
    <conferenceType>1</conferenceType>
    <invitees>
        <invitee>
            <rtxAccount>被邀请人1的RTX账号</rtxAccount>
            <tel>被邀请人1电话号码</tel>
        </invitee>
        <invitee>
            <rtxAccount>被邀请人2的RTX账号</rtxAccount>
            <tel>被邀请人2电话号码</tel>
        </invitee>
    </invitees>
</RTXConferenceReqDTO>
转化之后的关联数组:
Array
(
    [conferenceTitle] => IT交流会 www.th7.cn
    [startTime] => 2011-12-19 12:00:00
    [rtxAccount] => andy1111111
    [ipAddr] => 192.168.1.56
    [duration] => 120
    [conferenceType] => 1
    [invitees] => Array
        (
            [invitee] => Array
                (
                    [0] => Array
                        (
                            [rtxAccount] => 被邀请人1的RTX账号
                            [tel] => 被邀请人1电话号码
                        )
 
                    [1] => Array
                        (
                            [rtxAccount] => 被邀请人2的RTX账号
                            [tel] => 被邀请人2电话号码
                        )
 
                )
 
        )
 
)
     */ 
    function xml2array($xmlString = '') 
    { 
        $targetArray = array(); 
        $xmlObject = simplexml_load_string($xmlString); 
        $mixArray = (array)$xmlObject; 
        foreach($mixArray as $key => $value) 
        { 
            if(is_string($value)) 
            { 
                $targetArray[$key] = $value; 
            } 
            if(is_object($value)) 
            { 
                $targetArray[$key] = xml2array($value->asXML()); 
            } 
            if(is_array($value)) 
            { 
                foreach($value as $zkey => $zvalue) 
                { 
                    if(is_numeric($zkey)) 
                    { 
                        $targetArray[$key][] = xml2array($zvalue->asXML()); 
                    } 
                    if(is_string($zkey)) 
                    { 
                        $targetArray[$key][$zkey] = xml2array($zvalue->asXML()); 
                    } 
                } 
            } 
        } 
        return $targetArray; 
         
    } 


摘自  andy1219111的专栏

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索xml , 的 计划 ,以便于您获取更多的相关知识。

弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率

40+云计算产品,6个月免费体验

稳定可靠、可弹性伸缩的在线数据库服务,全球最受欢迎的开源数据库之一

云服务器9.9元/月,大学必备