douqu2712 2014-03-31 21:43
浏览 57
已采纳

XML数据到PHP

I'm trying to get XML data from a URL. When I print the code below, the variable $name does not print. Any ideas on why the XML data is not being parsed into the $name variable? Thanks in advanced!

$doc = new DOMDocument();  
$doc->load(simplexml_load_file("http://api.remix.bestbuy.com/v1/products(name=$new*)?apiKey=API-KEY&sort=salesRankLongTerm.asc"));
  ## lets read the code block
  $records = $doc->getElementsByTagName( "product" );
  foreach( $records as $record )
  {
  ## parse the name
  $name = $record->getElementsByTagName( "name" );
  $name = $name->item(0)->nodeValue;
        echo $records;
  }

Piece of XML code from browser:

<product>
    <sku>6238297</sku>
    <productId>1218721416338</productId>
    <name>Apple® - MacBook Air® - 13.3" Display - 4GB Memory - 128GB Flash Storage</name>
    <source>bestbuy</source>
    <type>HardGood</type>
    <startDate>2013-06-11</startDate>
    <new>false</new>
    <active>true</active>
    <lowPriceGuarantee>true</lowPriceGuarantee>
    <activeUpdateDate>2014-03-05T23:36:23</activeUpdateDate>
    <regularPrice>1099.99</regularPrice>
    <salePrice>1099.99</salePrice>
    <onSale>false</onSale>
    <planPrice/>
    <priceWithPlan/>
    <priceRestriction/>
    <priceUpdateDate>2014-03-30T00:11:15</priceUpdateDate>
    <digital>false</digital>
    <preowned>false</preowned>
    <carrierPlans/>
    <technologyCode/>
    <carrierModelNumber/>
    <earlyTerminationFees/>
    <outletCenter/>
    <secondaryMarket/>
    <frequentlyPurchasedWith/>
    <accessories>
      <sku>9634885</sku>
      <sku>1019306</sku>
      <sku>5856129</sku>
      <sku>7016136</sku>
      <sku>7077111</sku>
      <sku>7016127</sku>
    </accessories>
    <relatedProducts>
      <sku>1307511053</sku>
      <sku>1307511077</sku>
      <sku>1306954028</sku>
      <sku>1306862453</sku>
      <sku>1307510025</sku>
    </relatedProducts>
    <techSupportPlans>
      <sku>1853259</sku>
    </techSupportPlans>
    <salesRankShortTerm>294</salesRankShortTerm>
    <salesRankMediumTerm>269</salesRankMediumTerm>
    <salesRankLongTerm>285</salesRankLongTerm>
    <bestSellingRank>297</bestSellingRank>
    <url>http://www.bestbuy.com/site/apple-macbook-air-13-3-display-4gb-memory-128gb-flash-storage/6238297.p?id=1218721416338&skuId=6238297&cmp=RMX&ky=2epk1kYlBfccbc5Dqglj3cTQUWJ0mBN4C</url>
    <spin360Url/>
    <mobileUrl>http://m.bestbuy.com/r/1218721416338/6238297/</mobileUrl>
    <affiliateUrl/>
    <addToCartUrl>http://www.bestbuy.com/site/olspage.jsp?id=pcmcat152200050035&type=category&cmp=RMX&ky=2epk1kYlBfccbc5Dqglj3cTQUWJ0mBN4C&qvsids=6238297</addToCartUrl>
    <affiliateAddToCartUrl/>
    <linkShareAffiliateUrl/>
    <linkShareAffiliateAddToCartUrl/>
    <upc>885909630882</upc>
    <productTemplate>Notebook_Computers</productTemplate>
    <categoryPath>
      <category>
        <id>cat00000</id>
        <name>Best Buy</name>
      </category>
      <category>
        <id>abcat0500000</id>
        <name>Computers & Tablets</name>
      </category>
      <category>
        <id>abcat0502000</id>
        <name>Laptops</name>
      </category>
      <category>
        <id>pcmcat247400050001</id>
        <name>MacBooks</name>
      </category>
    </categoryPath>
    <lists/>
    <customerReviewCount>922</customerReviewCount>
    <customerReviewAverage>4.8</customerReviewAverage>
    <format/>
    <freeShipping>true</freeShipping>
    <freeShippingEligible>true</freeShippingEligible>
    <inStoreAvailability>true</inStoreAvailability>
    <inStoreAvailabilityText>Store Pickup:</inStoreAvailabilityText>
    <inStoreAvailabilityUpdateDate>2014-03-05T23:36:23</inStoreAvailabilityUpdateDate>
    <itemUpdateDate>2014-03-31T09:13:14</itemUpdateDate>
    <onlineAvailability>true</onlineAvailability>
    <onlineAvailabilityText>Shipping: Usually leaves our warehouse in 1 business day</onlineAvailabilityText>
    <onlineAvailabilityUpdateDate>2014-03-05T23:36:23</onlineAvailabilityUpdateDate>
    <releaseDate>2013-06-20</releaseDate>
    <shippingCost>0.00</shippingCost>
    <shipping>
      <ground>0.00</ground>
      <secondDay>12.99</secondDay>
      <nextDay>26.99</nextDay>
      <vendorDelivery/>
    </shipping>
    <specialOrder>false</specialOrder>
    <shortDescription>4th Gen Intel&#174; Core&#8482; i5 processor; 13.3" display; 4GB memory; 128GB flash storageSpecial features: BluetoothDVD/CD drive not included</shortDescription>
    <class>MOBILE COMPUTING</class>
    <classId>140</classId>
    <subclass>APPLE</subclass>
    <subclassId>5075</subclassId>
    <department>COMPUTERS</department>
    <departmentId>6</departmentId>
    <buybackPlans/>
    <protectionPlans>
      <protectionPlan>
        <sku>9977898</sku>
        <name>3-Year Accidental Protection Plan - Geek Squad</name>
        <productId>1218228675955</productId>
        <type>PSP-PRP</type>
        <term>3YR</term>
        <regularPrice>499.98</regularPrice>
        <salePrice>499.98</salePrice>
      </protectionPlan>
      <protectionPlan>
        <sku>9975645</sku>
        <name>2-Year Accidental Protection Plan - Geek Squad</name>
        <productId>1218228680268</productId>
        <type>PSP-PRP</type>
        <term>2YR</term>
        <regularPrice>339.99</regularPrice>
        <salePrice>339.99</salePrice>
      </protectionPlan>
      <protectionPlan>
        <sku>9978139</sku>
        <name>1-Year Accidental Protection Plan - Geek Squad</name>
        <productId>1218228674882</productId>
        <type>PSP-PRP</type>
        <term>1YR</term>
        <regularPrice>199.99</regularPrice>
        <salePrice>199.99</salePrice>
      </protectionPlan>
    </protectionPlans>
    <productFamilies/>
    <description/>
    <manufacturer>Apple®</manufacturer>
    <modelNumber>MD760LL/A</modelNumber>
    <image>http://images.bestbuy.com/BestBuy_US/images/products/6238/6238297_sc.jpg</image>
    <largeFrontImage>http://images.bestbuy.com/BestBuy_US/images/products/6238/6238297_sa.jpg</largeFrontImage>
    <mediumImage>http://images.bestbuy.com/BestBuy_US/images/products/6238/6238297fp.gif</mediumImage>
    <thumbnailImage>http://images.bestbuy.com/BestBuy_US/images/products/6238/6238297_s.gif</thumbnailImage>
    <largeImage>http://images.bestbuy.com/BestBuy_US/images/products/6238/6238297_sb.jpg</largeImage>
    <alternateViewsImage>http://images.bestbuy.com/BestBuy_US/images/products/6238/6238297cv1a.jpg</alternateViewsImage>
    <angleImage/>
    <backViewImage/>
    <energyGuideImage/>
    <leftViewImage/>
    <accessoriesImage/>
    <remoteControlImage/>
    <rightViewImage/>
    <topViewImage/>
    <condition>New</condition>
    <inStorePickup>true</inStorePickup>
    <friendsAndFamilyPickup>true</friendsAndFamilyPickup>
    <homeDelivery>false</homeDelivery>
    <quantityLimit>1</quantityLimit>
    <fulfilledBy>BestBuy.com</fulfilledBy>
    <bundledIn/>
    <color/>
    <depth>8.94"</depth>
    <dollarSavings>0.00</dollarSavings>
    <percentSavings>0.0</percentSavings>
    <tradeInValue/>
    <height>0.11"–0.68"</height>
    <orderable>Available</orderable>
    <weight>2.96 lbs.</weight>
    <shippingWeight>6.45</shippingWeight>
    <width>12.8"</width>
    <warrantyLabor>1 year limited</warrantyLabor>
    <warrantyParts>1 year limited</warrantyParts>
    <longDescription>The new MacBook Air features 4th Gen Intel&#174; Core&#8482; processors with faster graphics, all-day battery life,1 and even faster flash storage.2 It's thin, light, and durable enough to take everywhere you go &#8212; and powerful enough to do everything once you get there.</longDescription>
    <includedItemList/>
    <marketplace>false</marketplace>
    <listingId/>
    <sellerId/>
    <shippingRestrictions/>
    <bluRayPlayer>false</bluRayPlayer>
    <bluetoothEnabled>true</bluetoothEnabled>
    <displayType>1440 x 900 pixel LED-backlit glossy</displayType>
    <energyStarQualified>true</energyStarQualified>
    <screenSizeIn>13.3</screenSizeIn>
  </product>
  • 写回答

2条回答 默认 最新

  • dongshadu4498 2014-03-31 22:00
    关注

    You are close to the solution:

    • You should not call simplexml_load_file inside DomDocument->load. These functions belong to two different libraries and generally only one of them is preferred.
    • You are finding elements with tag name "product" into an array. But in your XML there is only one "product" tag. So you do not need to iterate over $products.

    Try the following:

    $xml = file_get_contents("your URL");
    $doc = new DOMDocument();
    $doc->loadXML($xml);
    $records = $doc->getElementsByTagName( "product" );
    $name = $records->item(0)->getElementsByTagName( "name" );
    $name = $name->item(0)->nodeValue;
    echo $name;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果