2013-12-11 33 views
-2

我有一個文件的XML,但我有兩個第一線奇怪,與「< S:」 我想在PHP中讀取XML數據的「< OrderList> 」。 我有搜索谷歌和其他關於肥皂,但沒有任何作品。我嘗試過,simplexml_load_file()和新的DomDocument()來解析數據... snif。XML讀數值S:信封S:身體

謝謝你的幫助。

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s:Body> 
     <GetOrderListResponse xmlns="http://www.cdiscount.com"> 
     <GetOrderListResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
      <ErrorMessage i:nil="true" xmlns="http://schemas.datacontract.org/2004/07/Cdiscount.Framework.Core.Communication.Messages"/> 
      <OperationSuccess xmlns="http://schemas.datacontract.org/2004/07/Cdiscount.Framework.Core.Communication.Messages">true</OperationSuccess> 
      <ErrorList/> 
      <SellerLogin>login</SellerLogin> 
      <TokenId>???</TokenId> 
      <OrderList> 
       <Order> 
        <ArchiveParcelList>false</ArchiveParcelList> 
        <InitialTotalAmount>3.7</InitialTotalAmount> 
        <OrderLineList> 
        <OrderLine> 
         <AcceptationState>RefusedBySeller</AcceptationState> 
         <CategoryCode>06010701</CategoryCode> 
         <ProductEan></ProductEan> 
         <ProductId>3275054001106</ProductId> 
         <PurchasePrice>1.2</PurchasePrice> 
         <Quantity>1</Quantity> 
         <SellerProductId>REF3275054001</SellerProductId> 
         <Sku>3275054001106</Sku> 
         <SkuParent i:nil="true"/> 
         <UnitShippingCharges>2.5</UnitShippingCharges> 
        </OrderLine> 
        </OrderLineList> 
       </Order> 
      </OrderList> 
     </GetOrderListResult> 
     </GetOrderListResponse> 
    </s:Body> 
</s:Envelope> 

回答

0

XML名稱空間也是識別元素/屬性屬於哪種格式的一種方法。

s:是一個名稱空間別名,在這種情況下,根據根elmement上的xmlns:s屬性定義的名稱空間http://schemas.xmlsoap.org/soap/envelope/。所以s:Envelopes:Body位於soap命名空間中。

GetOrderListResponse也具有xmlns屬性。這將不帶前綴的元素的名稱空間更改爲http://www.cdiscount.com

這是肥皂,所以使用Soap extension類將是一個好主意。

如果您喜歡使用DOM並直接查詢數據,則必須考慮名稱空間。

$dom = new DOMDocument(); 
$dom->loadXml($xml); 
$xpath = new DOMXpath($dom); 
// register OWN namespace aliases for the xpath 
$xpath->registerNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/'); 
$xpath->registerNamespace('cd', 'http://www.cdiscount.com'); 

// get all order nodes in "http://www.cdiscount.com" namespace 
foreach ($xpath->evaluate('//cd:Order', NULL, FALSE) as $order) { 
    // fetch the InitialTotalAmount as a number 
    var_dump($xpath->evaluate('number(cd:InitialTotalAmount)', $order, FALSE)); 
} 

輸出:

float(3.7) 
相關問題