无法使用 SimpleXML 读取 XML
我通过 cUrl 接收使用 PHP 生成的 XML,代码如下:
$c = curl_init("http://www.domain.com/script.php");
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$xmlstr = curl_exec($c);
如果我回显 $xmlstr 变量,它会显示以下内容:
<?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>
这是正确的,并且与脚本中生成的相同。但是,如果我尝试执行以下操作,
$xml = simplexml_load_string($xmlstr);
if (!is_object($xml))
throw new Exception('Reading XML error',1001);
echo $xml->e1->minutos;
则不显示任何内容并且 print_r 对象将打印一个空对象。可能出什么问题了?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有两个主要问题,首先您没有考虑任何名称空间(例如soap),其次您没有遍历XML 层次结构来获取所需的元素。
“简单”的方法是:
您还可以对值进行 XPath 查询:
对于它的价值,PHP 手册包含 基本用法示例,涵盖如何遍历
children()
和registerXPathNamespace()
通过示例向您展示如何使用命名空间元素。最后,正如您所看到的,带有 SimpleXML 的
print_r()
/var_dump()
的输出并不总是很有帮助!查看所拥有内容的最佳方法是 echosaveXML()
这将显示给定元素的 XML。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:
You could also make an XPath query for the value:
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()
andregisterXPathNamespace()
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 echosaveXML()
which will display the XML for a given element.尝试子方法:
Try the children method: