2013-03-27 20 views
1

有人可以幫助我得到以下XML的第二個孩子:如何獲取XML二孩

<?xml version="1.0" encoding="UTF-8"?> 
<GetItemResponse xmlns="urn:ebay:apis:eBLBaseComponents"> 
    <Timestamp>2013-03-27T03:39:01.575Z</Timestamp> 
    <Ack>Success</Ack> 
    <Version>815</Version> 
    <Build>E815_CORE_API_15855340_R1</Build> 
    <item> 
    <ApplicationData>881030.B.0000</ApplicationData> 
    <AutoPay>false</AutoPay> 
    <BuyerProtection>ItemEligible</BuyerProtection> 
    <BuyItNowPrice currencyID="USD">0.0</BuyItNowPrice> 
    <Country>US</Country> 
    <Currency>USD</Currency> 
    <GiftIcon>0</GiftIcon> 
    <HitCounter>RetroStyle</HitCounter> 
    <ItemID></ItemID> 
    <ListingDetails> 
     <Adult>false</Adult> 
     <BindingAuction>false</BindingAuction> 
     <CheckoutEnabled>true</CheckoutEnabled> 
     <ConvertedBuyItNowPrice currencyID="USD">0.0</ConvertedBuyItNowPrice> 
    <ShippingServiceOptions> 
      <ShippingService>UPSGround</ShippingService> 
      <ShippingServiceCost currencyID="USD">9.99</ShippingServiceCost> 
    </ShippingServiceOptions> 
    <InternationalShippingServiceOption> 
      <ShippingService>StandardInternational</ShippingService> 
      <ShippingServiceCost currencyID="USD">39.99</ShippingServiceCost> 
    </InternationalShippingServiceOption> 
    <item> 

我使用了一下,以循環通過所有的項目中(爲$項目爲$項目)。我需要從ShippingServiceOptions和InternationalShippingServiceOption中獲取ShippingServiceCost。

我要做到以下幾點,但它不工作:

//for ShippingServiceOptions 
$item->getElementsByTagName('ShippingServiceCost')->item(0)->nodeValue; 

//for InternationalServiceOptions 
$item->getElementsByTagName('ShippingServiceCost')->item(1)->nodeValue; 
+0

我認爲這個問題是你不封閉'item'我不得不標籤 – luchosrock 2013-03-27 05:03:03

回答

0

編輯

,因爲你已經發布完整的XML PHP的XML遍歷看起來像:

$xml = simplexml_load_string($response); 
foreach($xml->item->ListingDetails as $child) { 
    foreach($child->children() as $option) { 
     if(isset($option->ShippingServiceCost)){ 
      echo $option->getName() . ": " . $option->ShippingServiceCost . "<br>"; 
     } 
    } 
} 

您發佈的XML有錯誤,日後請在發佈之前進行驗證,以免我們會修復錯誤:)


如果你有PHP 5 +,你可以使用simplexml來解析你的xml。 另外,您需要關閉您的「item」xml標籤。

然後代碼變爲:

<?php 
    $xml = simplexml_load_file("test.xml"); 
    foreach($xml->children() as $child) { 
     echo $child->getName() . ": " . $child->ShippingServiceCost . "<br>"; 
    } 
?> 

XML:

<item> 
<ShippingServiceOptions> 
     <ShippingService>UPSGround</ShippingService> 
     <ShippingServiceCost currencyID="USD">9.99</ShippingServiceCost> 
</ShippingServiceOptions> 
<InternationalShippingServiceOption> 
     <ShippingService>StandardInternational</ShippingService> 
     <ShippingServiceCost currencyID="USD">39.99</ShippingServiceCost> 
</InternationalShippingServiceOption> 
</item> 

輸出:

ShippingServiceOptions: 9.99 
InternationalShippingServiceOption: 39.99 
+0

使用ole $ dom = new DOMDocument(); $ dom-> loadXML($ response); – user2201011 2013-03-27 05:02:56

+0

然後使用$ xml = simplexml_load_string($ response);而不是simplexml_load_file('url.xml') – Mupps 2013-03-27 05:08:16

+0

嗯,在我的原始文章中,我簡化了XML的一個小問題。我已經發布了完整的XML。 foreach($ xml-> children()作爲$ child)返回timestamp,ack,version,build – user2201011 2013-03-27 13:11:46