2013-12-11 26 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) 
相关问题