PHP - 将XML转换为数组错过了XML字符串中的一些信息

时间:2016-03-23 14:48:45

标签: php arrays xml simplexml simplexml-load-string

我使用curl获取外部XML文档并将其转换为数组。

代码工作得非常完美,除了XML中的一个节点没有被处理并放入我的数组中。

<drivers>下,每个驱动程序都有一个代码:<driver code="TM1">但我的数组不会将其作为@attribute数组(例如<collection date="20160324">

有人知道为什么会这样吗?

XML:

<findit xmlns="http://www.URL.COM" version="8" type="TIX" date="20160323">
  <account code="XXXXXX">
    <customers>
      <customer code="12345">
        <status code="0">Success</status>
        <logistic-jobs>
          <logistic-job id="12345" date="20160324" status="PLA" modified="201603231420">
        <number>
          <number1>479599</number1>
          <number3>11221</number3>
        </number>
        <collection date="20160324" time="0500">
          <name>JOHN SMITH</name>
          <address1>UNIT 3 DAVEY ROAD</address1>
          <address2>FIELDS END BUSINESS PARK</address2>
          <address3>GOLDTHORPE</address3>
          <address4>ROTHERHAM</address4>
          <address5>S63 0JF</address5>
        </collection>
        <delivery date="20160324" time="1200">
          <address1>EXAMPLE</address1>
          <address2>GLENEAFLES FARM</address2>
          <address3>GLENEAGLES CLOSE</address3>
          <address4>STANWELL, MIDDLESEX</address4>
          <address5>TW19 7PD</address5>
        </delivery>
        <extra>
          <address1>45FT C/SIDER</address1>
          <address2>No</address2>
          <address4>CEMENT</address4>
        </extra>
        <drivers>
          <driver code="TM1">DAVE SMITH (DAYS)</driver>
        </drivers>
        <load weight="27600.00" volume="0.00">
          <pallets full="23" half="0" quarter="0" blue="0" oversize="0"/>
        </load>
      </logistic-job>
    </logistic-jobs>
  </customer>
</customers>
</account>
</findit>

PHP:

$job_array = json_decode(json_encode(simplexml_load_string($xml)), true);

if(is_array($job_array['account']['customers']['customer'])) {

    // Foreach customer in array
    foreach($job_array['account']['customers']['customer'] as $i => $customer) {

        // If status is set to success
        if($customer['status'] == "Success") {

            // For each job
            foreach($customer['logistic-jobs']['logistic-job'] as $i => $job) {

                echo '<pre>'; print_r($job); echo '</pre>';

            }

        }

    }

}

输出:

Array
(
[@attributes] => Array
    (
        [id] => 12345
        [date] => 20160324
        [status] => PLA
        [modified] => 201603231420
    )

[number] => Array
    (
        [number1] => 479599
        [number3] => 11221
    )

[collection] => Array
    (
        [@attributes] => Array
            (
                [date] => 20160324
                [time] => 0500
            )

        [name] => JOHN SMITH
        [address1] => UNIT 3 DAVEY ROAD
        [address2] => FIELDS END BUSINESS PARK
        [address3] => GOLDTHORPE
        [address4] => ROTHERHAM
        [address5] => S63 0JF
    )

[delivery] => Array
    (
        [@attributes] => Array
            (
                [date] => 20160324
                [time] => 1200
            )

        [address1] => EXAMPLE
        [address2] => GLENEAFLES FARM
        [address3] => GLENEAGLES CLOSE
        [address4] => STANWELL, MIDDLESEX
        [address5] = TW19 7PD
    )

[extra] => Array
    (
        [address1] => 45FT C/SIDER
        [address2] => No
        [address4] => CEMENT
    )

[drivers] => Array
    (
        [driver] => DAVE SMITH (DAYS)
    )

[load] => Array
    (
        [@attributes] => Array
            (
                [weight] => 21509.00
                [volume] => 0.00
            )

        [pallets] => Array
            (
                [@attributes] => Array
                    (
                        [full] => 52
                        [half] => 0
                        [quarter] => 0
                        [blue] => 0
                        [oversize] => 0
                    )

            )

    )

)

1 个答案:

答案 0 :(得分:3)

我有一个简单的答案:不要将XML转换为数组。 SimpleXML有一个非常有用的API,用于遍历XML文档并查找所需的数据;丢掉可怕的json_decode(json_encode(黑客并查看examples in the PHP manual

在这种情况下,echo $driver将为您提供内容(驱动程序名称),echo $driver['code']将为您提供属性值;很明显,一个简单的数组不具备那种方便的魔法,这就是为什么转换为一个会给你带来麻烦。

请记住,print_r也会隐藏您的内容,您可能需要a dedicated debugging function

以下是获取每个驱动程序的代码和名称的示例(包含live demo):

$job_simple = simplexml_load_string($xml);

if(isset($job_simple->account->customers->customer)) {

    // Foreach customer in array
    foreach($job_simple->account->customers->customer as $i => $customer) {

        // If status is set to success
        if((string)$customer->status == "Success") {

            // For each job
            foreach($customer->{'logistic-jobs'}->{'logistic-job'} as $i => $job) {
                echo "<ul>\n";
                foreach ( $job->drivers->driver as $driver ) {
                     echo "<li> {$driver['code']}: $driver\n";
                }
                echo "</ul>\n";
            }
        }
    }
}