2011-05-29 58 views
0

这里是xml的片段,它是一种我从未见过的格式,对于我的生活,我无法弄清楚如何将它解析成实际看起来像样的东西一个网站。这里是xml。这是shopping.com API上的搜索查询的结果。用php解析奇怪格式化的XML

object(SimpleXMLElement)#14 (11) { 
    ["@attributes"]=> 
    array(1) { 
    ["id"]=> 
    string(9) "101677489" 
    } 
    ["name"]=> 
    string(26) "Nikon D3100 Digital Camera" 
    ["shortDescription"]=> 
    string(87) "14.2 Megapixel, SLR Camera, 3 in. LCD Screen, With High Definition Video, Weight: 1 lb." 
    ["fullDescription"]=> 
    string(829) "The Nikon D3100 digital SLR camera speaks to the growing ranks of enthusiastic D-SLR users and aspiring photographers by providing an easy-to-use and affordable entrance to the world of Nikon D-SLR’s. The 14.2-megapixel D3100 has powerful features, such as the enhanced Guide Mode that makes it easy to unleash creative potential and capture memories with still images and full HD video. Like having a personal photo tutor at your fingertips, this unique feature provides a simple graphical interface on the camera’s LCD that guides users by suggesting and/or adjusting camera settings to achieve the desired end result images. The D3100 is also the world’s first D-SLR to introduce full time auto focus (AF) in Live View and D-Movie mode to effortlessly achieve the critical focus needed when shooting Full HD 1080p video." 
    ["images"]=> 
    object(SimpleXMLElement)#4 (1) { 
    ["image"]=> 
    array(5) { 
     [0]=> 
     object(SimpleXMLElement)#13 (2) { 
     ["@attributes"]=> 
     array(3) { 
      ["available"]=> 
      string(4) "true" 
      ["height"]=> 
      string(3) "100" 
      ["width"]=> 
      string(3) "100" 
     } 
     ["sourceURL"]=> 
     string(131) "http://di1.shopping.com/images/pi/93/bc/04/101677489-100x100-0-0.jpg?p=p2.6566ad68b022f6260efc&a=2&c=1&l=7000610&t=110528211343&r=2" 
     } 
     [1]=> 
     object(SimpleXMLElement)#5 (2) { 
     ["@attributes"]=> 
     array(3) { 
      ["available"]=> 
      string(4) "true" 
      ["height"]=> 
      string(3) "200" 
      ["width"]=> 
      string(3) "200" 
     } 
     ["sourceURL"]=> 
     string(131) "http://di1.shopping.com/images/pi/93/bc/04/101677489-200x200-0-0.jpg?p=p2.6566ad68b022f6260efc&a=2&c=1&l=7000610&t=110528211343&r=2" 
     } 
     [2]=> 
     object(SimpleXMLElement)#12 (2) { 
     ["@attributes"]=> 
     array(3) { 
      ["available"]=> 
      string(4) "true" 
      ["height"]=> 
      string(3) "300" 
      ["width"]=> 
      string(3) "300" 
     } 
     ["sourceURL"]=> 
     string(131) "http://di1.shopping.com/images/pi/93/bc/04/101677489-300x300-0-0.jpg?p=p2.6566ad68b022f6260efc&a=2&c=1&l=7000610&t=110528211343&r=2" 
     } 
     [3]=> 
     object(SimpleXMLElement)#11 (2) { 
     ["@attributes"]=> 
     array(3) { 
      ["available"]=> 
      string(4) "true" 
      ["height"]=> 
      string(3) "400" 
      ["width"]=> 
      string(3) "400" 
     } 
     ["sourceURL"]=> 
     string(131) "http://di1.shopping.com/images/pi/93/bc/04/101677489-400x400-0-0.jpg?p=p2.6566ad68b022f6260efc&a=2&c=1&l=7000610&t=110528211343&r=2" 
     } 
     [4]=> 
     object(SimpleXMLElement)#10 (2) { 
     ["@attributes"]=> 
     array(3) { 
      ["available"]=> 
      string(4) "true" 
      ["height"]=> 
      string(3) "500" 
      ["width"]=> 
      string(3) "606" 
     } 
     ["sourceURL"]=> 
     string(131) "http://di1.shopping.com/images/pi/93/bc/04/101677489-606x500-0-0.jpg?p=p2.6566ad68b022f6260efc&a=2&c=1&l=7000610&t=110528211343&r=2" 
     } 
    } 
    } 
    ["rating"]=> 
    object(SimpleXMLElement)#7 (4) { 
    ["reviewCount"]=> 
    string(1) "5" 
    ["rating"]=> 
    string(4) "5.00" 
    ["ratingImage"]=> 
    object(SimpleXMLElement)#9 (2) { 
     ["@attributes"]=> 
     array(2) { 
     ["height"]=> 
     string(2) "18" 
     ["width"]=> 
     string(2) "91" 
     } 
     ["sourceURL"]=> 
     string(48) "http://img.shopping.com/sc/pr/sdc_stars_sm_5.gif" 
    } 
    ["reviewURL"]=> 
    string(61) "http://www.shopping.com/Nikon-D3100/reviews~linkin_id-7000610" 
    } 
    ["minPrice"]=> 
    string(6) "519.00" 
    ["maxPrice"]=> 
    string(6) "712.00" 
    ["productOffersURL"]=> 
    string(60) "http://www.shopping.com/Nikon-D3100/prices~linkin_id-7000610" 
    ["productSpecsURL"]=> 
    string(58) "http://www.shopping.com/Nikon-D3100/info~linkin_id-7000610" 
    ["offers"]=> 
    object(SimpleXMLElement)#6 (1) { 
    ["@attributes"]=> 
    array(3) { 
     ["matchedOfferCount"]=> 
     string(2) "10" 
     ["pageNumber"]=> 
     string(1) "1" 
     ["returnedOfferCount"]=> 
     string(1) "0" 
    } 
    } 

任何想法?我已经尝试了以下代码,但它不会改变任何内容。

foreach($xml->categories->category->items->product as $product) 
{ 
var_dump($product); 
} 
foreach($xml->categories->category->items->offer as $offer) 
{ 
var_dump($offer); 
} 
+0

的XML已解析* *。你有什么问题? *提取*必要的信息?那时会有哪些信息? – deceze 2011-05-29 01:28:49

+0

因此它被解析?是的,我想提取必要的信息。这是我用来获取信息的代码 $ x = file_get_contents($ url); $ xml = simplexml_load_string($ x); foreach($ xml-> categories-> category-> items-> product as $ product) { var_dump($ product); ($ xml-> categories-> category-> items-> offer as $ offer) { var_dump($ offer); } > 我想显示一个价格每个产品和图片等。据我了解什么是返回应该是这样的:?http://sandbox.api.shopping.com/publisher/ 3.0 /休息/ GeneralSearch?apiKey =授权密钥&trackingId = 7000610&关键字=尼康。 – Ryan 2011-05-29 01:31:11

+1

我建议你阅读[var_dump](http://www.php.net/manual/en/function.var-dump.php),这样你就知道它实际上是在输出什么...... – jswolf19 2011-05-29 01:41:23

回答

0

试着这么做:

foreach($xml->categories->category->items->product as $product) 
{ 
    echo "{$product->name} from {$product->minPrice} to {$product->maxPrice}\n"; 
} 
+0

这适用于部分我想要做什么,我应该能够找出其余的。所以,如果我正在阅读这个权利,它会读取每个产品并回应名称,最大值和最小价格? – Ryan 2011-05-29 01:38:57

+0

@瑞恩:是的。每个子节点可以以类似的方式访问(例如:'$ product-> image [0] - > sourceURL')。注意:如果您发现该答案有用,请立即投票(如果解决了问题,请接受该答案)。 :) – netcoder 2011-05-29 01:47:25

+0

神奇,Upvoted和接受,谢谢一吨。 – Ryan 2011-05-29 01:54:21