使用php从xml文件中提取数据

时间:2012-08-28 13:37:50

标签: php xml xpath

我知道之前已经问过这个问题,我已经尝试了一些建议,但我没有从XML文件中获取信息。我需要从xml文件中获取卖家的数量(OfferListingCount condition =“Any”)。这是XML:

    <?xml version="1.0"?>
<GetCompetitivePricingForASINResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
<GetCompetitivePricingForASINResult ASIN="0312479743" status="Success">
<Product xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
<Identifiers>
  <MarketplaceASIN>
    <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
    <ASIN>0312479743</ASIN>
  </MarketplaceASIN>
</Identifiers>
<CompetitivePricing>
  <CompetitivePrices>
    <CompetitivePrice belongsToRequester="false" condition="Used" subcondition="Good">
      <CompetitivePriceId>2</CompetitivePriceId>
      <Price>
        <LandedPrice>
          <CurrencyCode>USD</CurrencyCode>
          <Amount>36.23</Amount>
        </LandedPrice>
        <ListingPrice>
          <CurrencyCode>USD</CurrencyCode>
          <Amount>36.23</Amount>
        </ListingPrice>
        <Shipping>
          <CurrencyCode>USD</CurrencyCode>
          <Amount>0.00</Amount>
        </Shipping>
      </Price>
    </CompetitivePrice>
  </CompetitivePrices>
  <NumberOfOfferListings>
    <OfferListingCount condition="Any">34</OfferListingCount>
    <OfferListingCount condition="Used">29</OfferListingCount>
    <OfferListingCount condition="New">5</OfferListingCount>
  </NumberOfOfferListings>

这是我对Php的更新代码:

    $priceComp_xml = amazonCompPrice_xml($asin);
$compPricing = $priceComp_xml->xpath('/OfferListingCount[@condition="Any"]');

$ priceComp_xml返回xml文件中的信息,我正在尝试使用下一行来获取我需要的信息。当我运行代码时,得到一个空数组。 我如何获得这些信息?

1 个答案:

答案 0 :(得分:0)

http://php.net/manual/en/function.simplexml-load-file.php

$xml = simplexml_load_file($xml_file);

        $output = array();
        foreach ($xml as $row => $item_data)
        {
            // Attributes
            $attributes =  $item_data->attributes();

加载它,获取属性。