2014-02-17 109 views
0

我正在做一個SOAP調用並獲取以XML格式返回的數據。返回的XML有一個標記,我不知道該如何處理。我只需要全部<web_get_debiteuren>php從XML文件獲取數據

我想過使用php SimpleXMLElement(http://www.php.net/manual/en/simplexml.examples-basic.php)。但我不能做這樣的事:

$xml = new SimpleXMLElement($result); 
echo $xml->soap; 

我如何將能夠通過所有結果瓦茲XML文件的<web_get_debiteuren>一部分?

參見下面的XML:

<?xml version="1.0" encoding="windows-1250"?> 
<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> 
    <getdatawithoptionsresponse xmlns="urn:Afas.Profit.Services"> 
     <getdatawithoptionsresult> 
     <AfasGetConnector> 
      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
      <xs:element name="AfasGetConnector"> 
       <xs:complexType> 
       <xs:choice maxOccurs="unbounded"> 
        <xs:element name="web_get_debiteuren"> 
        <xs:complexType> 
         <xs:sequence> 
         <xs:element name="Nummer_debiteur" type="xs:string" minOccurs="0"/> 
         <xs:element name="Naam_debiteur" type="xs:string" minOccurs="0"/> 
         <xs:element name="E-mail_werk" type="xs:string" minOccurs="0"/> 
         <xs:element name="Voornaam" type="xs:string" minOccurs="0"/> 
         <xs:element name="Achternaam" type="xs:string" minOccurs="0"/> 
         </xs:sequence> 
        </xs:complexType> 
        </xs:element> 
       </xs:choice> 
       </xs:complexType> 
      </xs:element> 
      </xs:schema> 
      <web_get_debiteuren> 
      <Nummer_debiteur>10000</Nummer_debiteur> 
      <Naam_debiteur>Test</Naam_debiteur> 
      <Voornaam>Hans</Voornaam> 
      <Achternaam>Klok</Achternaam> 
      </web_get_debiteuren> 
      <web_get_debiteuren> 
      <Nummer_debiteur>11000</Nummer_debiteur> 
      <Naam_debiteur>Sven</Naam_debiteur> 
      <E-mail_werk>[email protected]</E-mail_werk> 
      <Voornaam>Sven</Voornaam> 
      <Achternaam>Kramer</Achternaam> 
      </web_get_debiteuren> 
      <web_get_debiteuren> 
      <Nummer_debiteur>11001</Nummer_debiteur> 
      <Naam_debiteur>Ireen</Naam_debiteur> 
      <E-mail_werk>[email protected]</E-mail_werk> 
      <Voornaam>Ireen</Voornaam> 
      <Achternaam>Wust</Achternaam> 
      </web_get_debiteuren> 
     </AfasGetConnector> 
     </getdatawithoptionsresult> 
    </getdatawithoptionsresponse> 
    </soap:body> 
</soap:envelope> 
+0

如果您使用的是soap,返回應該是對象/數組形式。你如何獲得原始的XML?通過__getLastResponse()之類的調試功能之一? –

+0

__getLastResponse()爲空。 SOAP服務需要NTLM身份驗證,因此結果是由cURL提取的。我發佈的XML是從SOAP調用返回的代碼。 – Timo002

回答

0

您可以使用xpath。樣品中可能出現的問題:

Using xpath on a PHP SimpleXML object, SOAP + namespaces (not working..) parse an XML with SimpleXML which has multiple namespaces

但我認爲,你應該如果有可能使用SoapClient的..

+0

SoapClient不會返回任何東西。 SOAP服務需要NTLM身份驗證,因此結果是由cURL提取的。我發佈的XML是從SOAP調用返回的代碼。很難理解xpath,但看着它! – Timo002

+0

你可以試試這個解決方案 - http://www.php.net/manual/en/soapclient.soapclient.php#97029 – Nostalgie

+0

我用這個類,http://tcsoftware.net/blog/2011/08/php- soapclient-authentication /,我認爲也是這樣! – Timo002

0

由於懷舊的使用XPath的技巧中,我得到它的工作。請參見下面的代碼:

$response = simplexml_load_string($result ,NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/"); 

$response->registerXPathNamespace("site", "urn:Afas.Profit.Services"); 
$_res = $response->xpath('//site:AfasGetConnector'); 
#$_res = $_res->AfasGetConnector; 

foreach($_res[0]->web_get_debiteuren AS $deb){ 
    echo "Number: ".$deb->Nummer_debiteur."<br>"; 
} 

這將輸出:

Number: 10000 
Number: 11000 
Number: 11001 
Number: 11002 
Number: 11003 
Number: 11004 

感謝名單!