【发布时间】:2011-05-29 01:25:29
【问题描述】:
这里是 xml 的 sn-p,它是我从未见过的格式,在我的一生中,我无法弄清楚如何将它解析成在网站上看起来确实不错的东西。这是xml。这是在 shopping.com api 上搜索查询的结果。
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);
}
【问题讨论】:
-
XML 已经解析。你有什么问题? 提取必要的信息?那会是什么信息呢?
-
所以解析了?是的,我想提取必要的信息。这是我用来获取信息的代码 $x = file_get_contents($url); $xml = simplexml_load_string($x); foreach($xml->categories->category->items->product as $product) { var_dump($product); } foreach( $xml->categories->category->items->offer as $offer ) { var_dump($offer); } ?> 我想用价格和图片等显示每个产品。据我了解,返回的内容应该是这样的:sandbox.api.shopping.com/publisher/3.0/rest/…。
-
我建议你阅读var_dump,这样你就知道它实际输出了什么......
标签: php xml xml-parsing