无法使用 SimpleXML 解析 Soap 响应
我使用本机soapclient() 来调用WSDL,但无法使用simplexml 来解析结果。我始终收到错误:
Warning: simplexml_load_string() [function.simplexml-load-string]:
Entity: line 1: parser error : Extra content at the end of the document
这看起来不像命名空间问题,并且我已经尝试过网站其他地方提到的补丁来从输入字符串中删除冒号。
编辑:
谢谢,戈登
,是的,你是对的。服务器是使用数据集的 Microsoft 站点。
如果我声明肥皂客户端并将跟踪设置为 true:
$soapClient = new soapclient($this->wsdlUrl,array('trace'=>true));
然后从响应中检索原始 XML:
$soapResult = $soapClient->GetScheduledSectors();
$responseXml = $soapClient->__getLastResponse();
$xml = simplexml_load_string($responseXml, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");
然后我得到:
SimpleXMLElement Object
(
[Body] => SimpleXMLElement Object
(
)
)
响应 XML 标头和第一个标签如下所示:
<?xml version='1.0'?>
<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>
<GetScheduledSectorsResponse xmlns="https://www.kulula.com/k3api/">
<GetScheduledSectorsResult msdata:SchemaSerializationMode="ExcludeSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:schema id="dsFlight" targetNamespace="http://tempuri.org/dsFlight.xsd" attributeFormDefault="qualified" elementFormDefault="qualified" xmlns:mstns="http://tempuri.org/dsFlight.xsd" xmlns="http://tempuri.org/dsFlight.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop">
<xs:element name="dsFlight" msdata:IsDataSet="true" msdata:UseCurrentLocale="true" msprop:ExecutionTime="1140">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded"/>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<dsFlight xmlns="http://tempuri.org/dsFlight.xsd">
<FlightSector diffgr:id="FlightSector1" msdata:rowOrder="0">
<FlightSectorId>1</FlightSectorId>
<FlightSectorName>JNBCPT</FlightSectorName>
<FromAirport>O.R. Tambo (Jo'burg)</FromAirport>
<ToAirport>Cape Town</ToAirport>
<DepartureAirportId>1</DepartureAirportId>
<ArrivalAirportId>2</ArrivalAirportId>
<AirlineCode>BA</AirlineCode>
<Ordernumber>100</Ordernumber>
<FlightZoneId>1</FlightZoneId>
<DepartureCountryCode>ZA</DepartureCountryCode>
<DepartureContinentCode>AF</DepartureContinentCode>
</FlightSector>
我还尝试注册以下命名空间:
$xml = simplexml_load_string($this->responseXml, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");
$xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$xml->registerXPathNamespace('xsd', 'http://www.w3.org/2001/XMLSchema');
$xml->registerXPathNamespace('diffgr', 'urn:schemas-microsoft-com:xml-diffgram-v1');
$xml->registerXPathNamespace('mstns', 'http://tempuri.org/dsFlight.xsd');
$xml->registerXPathNamespace('msprop', 'urn:schemas-microsoft-com:xml-msprop');
但仍然得到$xml 中的空对象
I'm using the native soapclient() to call a WSDL but I can't use simplexml to parse the result. I consistently get an error:
Warning: simplexml_load_string() [function.simplexml-load-string]:
Entity: line 1: parser error : Extra content at the end of the document
This doesn't look like a namespace problem and I have already tried the patch mentioned elsewhere on the site to remove colons from the input string.
EDIT:
Thanks, Gordon
Yes you're right. The server is a Microsoft site using datasets.
If I declare the soap client with trace set to true:
$soapClient = new soapclient($this->wsdlUrl,array('trace'=>true));
and then retrieving the raw XML from the response:
$soapResult = $soapClient->GetScheduledSectors();
$responseXml = $soapClient->__getLastResponse();
$xml = simplexml_load_string($responseXml, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");
then I get:
SimpleXMLElement Object
(
[Body] => SimpleXMLElement Object
(
)
)
The response XML header and first tag looks like this:
<?xml version='1.0'?>
<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>
<GetScheduledSectorsResponse xmlns="https://www.kulula.com/k3api/">
<GetScheduledSectorsResult msdata:SchemaSerializationMode="ExcludeSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:schema id="dsFlight" targetNamespace="http://tempuri.org/dsFlight.xsd" attributeFormDefault="qualified" elementFormDefault="qualified" xmlns:mstns="http://tempuri.org/dsFlight.xsd" xmlns="http://tempuri.org/dsFlight.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop">
<xs:element name="dsFlight" msdata:IsDataSet="true" msdata:UseCurrentLocale="true" msprop:ExecutionTime="1140">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded"/>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<dsFlight xmlns="http://tempuri.org/dsFlight.xsd">
<FlightSector diffgr:id="FlightSector1" msdata:rowOrder="0">
<FlightSectorId>1</FlightSectorId>
<FlightSectorName>JNBCPT</FlightSectorName>
<FromAirport>O.R. Tambo (Jo'burg)</FromAirport>
<ToAirport>Cape Town</ToAirport>
<DepartureAirportId>1</DepartureAirportId>
<ArrivalAirportId>2</ArrivalAirportId>
<AirlineCode>BA</AirlineCode>
<Ordernumber>100</Ordernumber>
<FlightZoneId>1</FlightZoneId>
<DepartureCountryCode>ZA</DepartureCountryCode>
<DepartureContinentCode>AF</DepartureContinentCode>
</FlightSector>
I have also tried to register the following namespaces:
$xml = simplexml_load_string($this->responseXml, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");
$xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$xml->registerXPathNamespace('xsd', 'http://www.w3.org/2001/XMLSchema');
$xml->registerXPathNamespace('diffgr', 'urn:schemas-microsoft-com:xml-diffgram-v1');
$xml->registerXPathNamespace('mstns', 'http://tempuri.org/dsFlight.xsd');
$xml->registerXPathNamespace('msprop', 'urn:schemas-microsoft-com:xml-msprop');
but still get an empty object in $xml
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试 DOMDocument 而不是 simpleXML,因为在我过去的使用中它似乎更容错。虽然没有那么快。
然而,当我需要解析 SOAP 时,我参考了 如何解析 SOAP XML?并找到了有关 PHP 5 本机 Soap 客户端的优秀信息。
You might try DOMDocument instead of simpleXML as it has seemed a bit more fault tolerent in my past usage. Not nearly as fast though.
When I needed to parse SOAP, however, I refferred to How to parse SOAP XML? and found excellent information on the PHP 5 native Soap client.
我有同样的问题,然后我尝试:
并查看结果。
I have same problem, then i try:
And see the results.
我也遇到这个问题。
在联系 Web 服务开发人员后,我得到的答案是该响应不是 XML,而是 DATATABLE(即 ac# 数据类型)。
最后我的解决方案是创建我自己的解析器,而不是使用本机 PHP 解析器。
根据相关字段尝试使用explode()。
我知道这不是最好的解决方案(绝对不是最佳实践),但是如果您无法以其他格式获取它,这可能对您有用。
I also encounter this issue.
After contacting the Web Service developer I got the answer that this response is not a XML, but DATATABLE (which is a c# datatype).
Finally my solution was to create my own parser, instead of using native PHP ones.
Try using explode() according to the relevant fields.
I'm know this is not the best solution (definitely not best practice), However if you can't get it in another format this might work for you.