1

有人可以帮我获得以下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>

我正在使用 for 循环浏览所有项目(对于 $items 作为 $item)。我需要从 ShippingServiceOptions 和 InternationalShippingServiceOption 获取 ShippingServiceCost。

我想执行以下操作,但它不起作用:

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

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

1 回答 1

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。此外,您需要关闭“项目”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
于 2013-03-27T04:59:35.150 回答