2012-02-14 40 views
4

我正在使用本機soapclient()來調用WSDL,但我無法使用simplexml來分析結果。我一直得到一個錯誤:無法使用SimpleXML分析肥皂響應

Warning: simplexml_load_string() [function.simplexml-load-string]: 
Entity: line 1: parser error : Extra content at the end of the document 

這看起來並不像一個命名空間的問題,我已經嘗試在網站上其他地方提到從輸入字符串中刪除冒號補丁。

編輯:

謝謝,戈登

是的,你說得對。該服務器是使用數據集的Microsoft網站。

如果我宣佈與跟蹤設置SOAP客戶端爲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一個空對象

+1

聽起來好像沒有根元素 – Gordon 2012-02-14 10:00:01

回答

1

你可以嘗試DOM文檔,而不是simplexml的,因爲它已顯得更有點故障在我以前的使用中容忍。雖然速度並不快。

當我需要解析SOAP時,我接受了How to parse SOAP XML?,並在PHP 5本機Soap客戶端上找到了很好的信息。

1

我有同樣的問題,然後我嘗試:

$soapResult = $soapClient->GetScheduledSectors(); 
$responseXml = $soapClient->__getLastResponse(); 

$dom = new DomDocument(); 
$dom->loadXML($responseXml); 

$phpObject = simplexml_load_string($dom->textContent); 
print_r($phpObject); 

,並查看結果。

0

我也遇到過這個問題。 聯繫Web服務開發者後,我得到了答案,這個響應不是XML,而是DATATABLE(它是一個C#數據類型)。

最後我的解決方案是創建我自己的解析器,而不是使用本機PHP。

請根據相關字段嘗試使用explode()。 我知道這不是最好的解決方案(絕對不是最佳做法),但是如果你不能以另一種格式獲得它,這可能適合你。