【问题标题】:complex XML to array in codeigniter复杂的 XML 到 codeigniter 中的数组
【发布时间】:2016-10-11 01:44:05
【问题描述】:

我得到了复杂的 XML 作为响应,我需要将其转换为数组,这是我的示例复杂 XML

   <?xml version="1.0" encoding="UTF-8"?>
 <Response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <SearchResult>
  <Result>
     <aaa>string</aaa>
     <bbb>string</bbb>
     <ccc>
        <ddd>
           <eee>string</eee>
           <fff>string</fff>
        </ddd>
        <ddd>
           <eee>string</eee>
           <fff>string</fff>
        </ddd>
     </ccc>
     <ggg>
        <hhh>
           <iii>string</iii>
           <eee>string</eee>
           <jjj>string</jjj>

        </hhh>
        <hhh>
           <iii>string</iii>
           <eee>string</eee>
           <jjj>string</jjj>

        </hhh>
     </ggg>
     <kkk>
        <hhh>
           <iii>string</iii>
           <eee>string</eee>
           <jjj>string</jjj>

        </hhh>
     </kkk>
     <lll>
        <mmm>string</mmm>
        <nnn>string</nnn>
        <ooo>string</ooo>

     </lll>

  </Result>


    <Result>
   //same values as above 
    </Result>



 <Result>
   //same values as above 
   </Result>

    </SearchResult> 
 </Response>

我尝试过简单的 XML,但它不起作用,

    function xml2Array($xmlstring)
{
    $xml = simplexml_load_string($xmlstring, "SimpleXMLElement", LIBXML_NOCDATA);
    $json = json_encode($xml);
    return json_decode($json,TRUE);
}

$arr = xml2Array($response);
print_r($arr);

但它返回空数组,请提出可能的解决方案

【问题讨论】:

标签: php arrays xml codeigniter


【解决方案1】:

作为参数传递一个 XML 格式的数据,然后转换为数组。

function xml2Array($xmlstring)
{

    $xml = simplexml_load_string($xmlstring, "SimpleXMLElement", LIBXML_NOCDATA);
    $json = json_encode($xml);
    return json_decode($json,TRUE);
}

$arr = xml2Array($response);
print_r($arr);

【讨论】:

    猜你喜欢
    • 2019-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多