drq9991 2013-08-27 11:22
浏览 11
已采纳

SimpleXML用于复杂XML的加载文件功能?

I am trying to use this function to get certain values back from the response.

The xml response is:

<details>

<ID>355499958</ID>
<parentID>94581</parentID>
<parentTable>acNumber</parentTable>
<title>Connected</title>
<StartTime>2013-08-20 12:30:54</StartTime>
<EndTime>2013-08-20 12:32:53</EndTime>
<connect>1.9902</connect>
<CLI>01234567890</CLI>
<dialledNumber>01234567890</dialledNumber>

<CData>
<DI N="opID" V="12345678" T="digitstring"/>
<DI N="account" V="1" T="digitstring"/>
<DI N="tID" V="1-2-3456789" T="digitstring"/>
<DI N="auth" V="test" T="digitstring"/>
<DI N="result" V="0" T="digitstring"/>
<DI N="accountID" V="" T="digitstring"/>
<DI N="tAmount" V="1000" T="digitstring"/>
<DI N="responseMessage" V="" T="digitstring"/>
</CData>

</details>

Now this is fine to pull back one of the first values etc: echo $xml->CLI;

But I need to get individual values from the CData fields.

How can I achieve this please?

  • 写回答

1条回答 默认 最新

  • drmy1050 2013-08-27 11:32
    关注

    You can do with simplexml_load_string or simplexml_load_file

    $xml = simplexml_load_string($xmlresponse);
    
    echo $xml->ID; // 355499958
    
    foreach($xml->CData->DI as $value)
    {
        $att = $value->attributes();
        echo $att['N']; // opID
        echo $att['V']; // 12345678
        echo $att['T']; // digitstring
    }
    

    Get specific index

    tAmount is 7. line. So 6. index in array. Array indexes starts with zero.

    $xml = simplexml_load_string($string);
    
    $data = $xml->CData->DI[6]->attributes();
    
    echo $data['N']; // tAmount
    echo $data['V']; // 1000
    echo $data['T']; // digitstring
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入