dongying9712 2011-06-07 18:35
浏览 29
已采纳

无法使用SimpleXML读取XML

I'm receiving through cUrl an XML generated with PHP with this code:

$c = curl_init("http://www.domain.com/script.php");
    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
    $xmlstr = curl_exec($c);

If I echo the $xmlstr variable it shows the following:

   <?xml version="1.0" encoding="utf-8"?>
        <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <soap:Body>
        <XGetPasoParadaREGResponse xmlns="http://tempuri.org/">
        <XGetPasoParadaREGResult>
        <PasoParada><cabecera>false</cabecera>
        <e1>
        <minutos>1</minutos>
        <metros>272</metros>
        <tipo>NORMAL</tipo>
        </e1>
        <e2>
        <minutos>7</minutos>
        <metros>1504</metros>
        <tipo>NORMAL</tipo>
        </e2><linea>28
        </linea>
        <parada>376</parada>
        <ruta>PARQUE ALCOSA</ruta>
        </PasoParada>
        </XGetPasoParadaREGResult><status>1</status></XGetPasoParadaREGResponse>
        </soap:Body>

</soap:Envelope>

Which is correct and the same generated at the script. However, if I try to do the following

$xml = simplexml_load_string($xmlstr);
    if (!is_object($xml))
        throw new Exception('Reading XML error',1001);
    echo $xml->e1->minutos;

Doesn't show anything and print_r the object prints an empty object. What could be wrong?

  • 写回答

2条回答 默认 最新

  • dqyz48470 2011-06-07 19:02
    关注

    You have two main problems, firstly you aren't taking into account any namespaces (e.g. soap) and secondly you aren't traversing the XML hierarchy to get at the desired element.

    The "simple" way would be:

    $minutos = (int) $xml
                         ->children('soap', TRUE)     // <soap:*>
                         ->Body                       // <soap:Body>
                         ->children('')               //   <*> (default/no namespace prefix)
                         ->XGetPasoParadaREGResponse  //   <XGetPasoParadaREGResponse>
                         ->XGetPasoParadaREGResult    //     <XGetPasoParadaREGResult>
                         ->PasoParada                 //       <PasoParada>
                         ->e1                         //         <e1>
                         ->minutos;                   //           <minutos> yay!
    

    You could also make an XPath query for the value:

    // Our target node belongs in this namespace
    $xml->registerXPathNamespace('n', 'http://tempuri.org/');
    // Fetch <minutos> elements children to <e1>
    $nodes = $xml->xpath('//n:e1/n:minutos');
    // $nodes will always be an array, get the first item
    $minutos = (int) $nodes[0];
    

    For what it is worth, the PHP Manual contains basic usage examples covering how to traverse the XML structure and the specific pages for children() and registerXPathNamespace() show you how to work with namespaced elements with examples.

    Finally, as you have seen, the output from print_r()/var_dump() with SimpleXML is not always very helpful at all! The best way to see what you've got is to echo saveXML() which will display the XML for a given element.

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

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站