Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Can';不能使用SimpleXML读取XML_Php_Xml_Simplexml - Fatal编程技术网

Php Can';不能使用SimpleXML读取XML

Php Can';不能使用SimpleXML读取XML,php,xml,simplexml,Php,Xml,Simplexml,我通过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"?> <soa

我通过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>
不显示任何内容并打印\r对象打印一个空对象。可能有什么问题?

请尝试:

请尝试以下方法:


您有两个主要问题,第一,您没有考虑任何名称空间(例如,
soap
),第二,您没有遍历XML层次结构以获得所需的元素

“简单”的方法是:

$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!
$minutos=(int)$xml
->儿童('soap',TRUE)//
->正文//
->子项(“”)/(默认/无命名空间前缀)
->xGetPasoparaderResponse//
->xGetPasoparadaRegressult//
->帕索帕拉达/
->e1//
->分钟数;//耶!
您还可以对值进行XPath查询:

// 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];
//我们的目标节点属于此命名空间
$xml->registerXPathNamespace('n','http://tempuri.org/');
//将元素的子元素提取到
$nodes=$xml->xpath('//n:e1/n:minutos');
//$nodes将始终是一个数组,获取第一项
$minutos=(int)$nodes[0];
值得一提的是,PHP手册涵盖了如何遍历的XML结构和特定页面,并通过示例向您展示了如何使用名称空间元素


最后,正如您所看到的,使用SimpleXML的
print\u r()
/
var\u dump()
的输出并不总是非常有用!查看所获得内容的最佳方法是回显哪个将显示给定元素的XML。

您有两个主要问题,第一个问题是没有考虑任何名称空间(例如,
soap
),第二个问题是没有遍历XML层次结构以获得所需的元素

“简单”的方法是:

$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!
$minutos=(int)$xml
->儿童('soap',TRUE)//
->正文//
->子项(“”)/(默认/无命名空间前缀)
->xGetPasoparaderResponse//
->xGetPasoparadaRegressult//
->帕索帕拉达/
->e1//
->分钟数;//耶!
您还可以对值进行XPath查询:

// 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];
//我们的目标节点属于此命名空间
$xml->registerXPathNamespace('n','http://tempuri.org/');
//将元素的子元素提取到
$nodes=$xml->xpath('//n:e1/n:minutos');
//$nodes将始终是一个数组,获取第一项
$minutos=(int)$nodes[0];
值得一提的是,PHP手册涵盖了如何遍历的XML结构和特定页面,并通过示例向您展示了如何使用名称空间元素


最后,正如您所看到的,使用SimpleXML的
print\u r()
/
var\u dump()
的输出并不总是非常有用!查看所获得内容的最佳方法是回显哪个元素将显示给定元素的XML。

当您
var_dump($XML)
时,您会得到什么?对象(SimpleXMLElement)#1(0){}但是字符串很好,如果保存为XML,则在每个浏览器中都会得到很好的解释。当您
var_dump($XML)
对象(SimpleXMLElement)#1(0)时,您会得到什么{}但是字符串很好,如果保存为XML,在每个浏览器中都会得到很好的解释。这就是它显示的内容:Body:object(simplexmlement){4(0){}object(simplexmlement){1(0){}这就是它显示的内容:Body:object(simplexmlement){4(0)}object object(simplexmlement){1(0){非常感谢!我真的不擅长解析XML,阅读的示例没有显示这种XML,所以我认为它们是一样的。非常感谢!我真的不擅长解析XML,阅读的示例没有显示这种XML,所以我认为它们是一样的。