dtmm0148603 2011-05-11 14:32
浏览 16
已采纳

在PHP中处理XML

I'm currently working a project that has me working with XML a lot. I have to take an XML response and decrypt each text node and then do various tasks with the data. The problem I'm having is taking the response and processing each text node. Originally I was using the XMLToArray library, and that worked fine I would change the XML into an array and then loop through the array and decrypt the values. However some of the XML response I'm dealing with have repeated tags and the XMLToArray library will only return the last values.

Is there a good way that I can take an XML response and process all the text nodes and easily putting the values into an array that has a similar structure to the response?

Thanks in advance.

  • 写回答

4条回答 默认 最新

  • douqi3195 2011-05-11 14:35
    关注

    I would use SimpleXML.

    Here's a small example of using it. It loads and parses XML from http://www.w3schools.com/xml/plant_catalog.xml and then outputs values of "COMMON" and "PRICE" tags of each "PLANT" tag.

    $xml = simplexml_load_file('http://www.w3schools.com/xml/plant_catalog.xml');
    foreach ( $xml->PLANT as $plantNode ) {
        echo $plantNode->COMMON, ' - ', $plantNode->PRICE, "
    ";
    }
    

    If you have any problems with adapting it to your needs, just give an example of your XML so that we can help with it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波