2011-05-29 79 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