Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何获取XML第二个子项_Php_Xml - Fatal编程技术网

Php 如何获取XML第二个子项

Php 如何获取XML第二个子项,php,xml,Php,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</

有人能帮我获取以下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>
编辑

由于您已经发布了完整的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>";
        }
    }
}

我必须使用ole$dom=newdomdocument()$dom->loadXML($response);然后使用$xml=simplexml\u load\u string($response);在我最初的帖子中,我将xml简化了一点,而不是simplexml加载文件('url.xml')。我已经发布了完整的XML。foreach($xml->children()as$child)返回timestamp、ack、version、build。我认为问题在于您未关闭的
标记
$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>";
        }
    }
}
<?php
    $xml = simplexml_load_file("test.xml");
    foreach($xml->children() as $child) {
        echo $child->getName() . ": " . $child->ShippingServiceCost . "<br>";
    }
?>
<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