简体   繁体   中英

PHP SimpleXML how do i get this value?

How would i go around retrieving the value of <TotalNew> in the example file below? I am unfamiliar with simplexml. I tried to access it as following: ItemLookupResponse->Items->Item->TotalNew but it did not produce a result.

Anyone who can point me out how to achieve this ? Thanks in advance

<?xml version="1.0"?>
<ItemLookupResponse  xmlns="http://webservices.amazon.com/AWSECommerceService/2011-08-01">
<OperationRequest>
<HTTPHeaders>
  <Header Name="UserAgent" Value="ApaiIO [2.0.0-DEV]"/>
</HTTPHeaders>
<RequestId>3ba918b6-7a00-4251-b24d-be8a40cb3eb8</RequestId>
<Arguments>
  <Argument Name="AWSAccessKeyId" Value=""/>
  <Argument Name="AssociateTag" Value=""/>
  <Argument Name="Condition" Value="New"/>
  <Argument Name="ItemId" Value=""/>
  <Argument Name="Operation" Value="ItemLookup"/>
  <Argument Name="ResponseGroup" Value="OfferSummary"/>
  <Argument Name="Service" Value="AWSECommerceService"/>
  <Argument Name="Timestamp" Value="2015-09-08T07:39:02Z"/>
  <Argument Name="Version" Value="2011-08-01"/>
  <Argument Name="Signature" Value="eglWsk68VxzDOE8PIdQdNget9RhZvPiBZylxpgn94ns="/>
</Arguments>
<RequestProcessingTime>0.0138980000000000</RequestProcessingTime>
</OperationRequest>
<Items>
<Request>
  <IsValid>True</IsValid>
  <ItemLookupRequest>
    <Condition>New</Condition>
    <IdType>ASIN</IdType>
    <ItemId></ItemId>
    <ResponseGroup>OfferSummary</ResponseGroup>
    <VariationPage>All</VariationPage>
  </ItemLookupRequest>
</Request>
<Item>
  <ASIN></ASIN>
  <ParentASIN></ParentASIN>
  <OfferSummary>
    <LowestNewPrice>
      <Amount>699</Amount>
      <CurrencyCode>USD</CurrencyCode>
      <FormattedPrice>$6.99</FormattedPrice>
    </LowestNewPrice>
    <LowestUsedPrice>
      <Amount>599</Amount>
      <CurrencyCode>USD</CurrencyCode>
      <FormattedPrice>$5.99</FormattedPrice>
    </LowestUsedPrice>
    <TotalNew>3</TotalNew>
    <TotalUsed>1</TotalUsed>
    <TotalCollectible>0</TotalCollectible>
    <TotalRefurbished>0</TotalRefurbished>
  </OfferSummary>
</Item>

Your XML has an error. Add the following to the end of your XML..

    </Items>
</ItemLookupResponse>

Then you can use the following.. ..bearing in mind that there may be multiple <item> elements inside <items> , so you will have to loop through them.

$xml = simplexml_load_string($string);

foreach ($xml->Items as $item){

    echo $item->Item->OfferSummary->TotalNew->__toString();

}

If you can guarantee that there will only ever be one <Item> inside <Items> , or you just want to target the first <Item> , you could achieve this without a loop, by targeting the first value of the <Items> array using Item[0] .

echo $xml->Items->Item[0]->OfferSummary->TotalNew->__toString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM