duanboniao5903 2016-03-23 14:48
浏览 44
已采纳

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

I am using curl to get an external XML document and convert it to an array.

The code works almost perfect, apart from one node in the XML isn't being processed and put in my array.

Under <drivers> there is a code for each driver: <driver code="TM1"> but this doesn't get picked up by my array as an @attribute array like the others such as <collection date="20160324">

Does anybody know why this is happening?

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>';

            }

        }

    }

}

OUTPUT:

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条回答

  • douyan2680 2016-03-23 17:00
    关注

    I have a simple answer for you: don't convert XML to an array. SimpleXML has a really useful API for traversing through the XML document and finding the data you want; throw away the horrible json_decode(json_encode( hack and look at the examples in the PHP manual.

    In this case, echo $driver would give you the contents (driver name) and echo $driver['code'] would give you the attribute value; clearly, a plain array can't have that convenient magic, which is why converting to one is giving you problems.

    Just remember that print_r will also hide things from you, and you might want a dedicated debugging function.

    Here's an example (with live demo) of getting the code and name of each driver:

    $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>
    ";
                    foreach ( $job->drivers->driver as $driver ) {
                         echo "<li> {$driver['code']}: $driver
    ";
                    }
                    echo "</ul>
    ";
                }
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?