用php解析格式奇怪的xml

用php解析格式奇怪的xml,php,xml,xml-parsing,Php,Xml,Xml Parsing,这里是xml的一个片段,它的格式是我从未见过的,就我的一生而言,我不知道如何将它解析成在网站上看起来不错的格式。下面是xml。这是shopping.com api上搜索查询的结果 object(SimpleXMLElement)#14 (11) { ["@attributes"]=> array(1) { ["id"]=> string(9) "101677489" } ["name"]=> string(26) "Nikon D3100 D

这里是xml的一个片段,它的格式是我从未见过的,就我的一生而言,我不知道如何将它解析成在网站上看起来不错的格式。下面是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);
}
尝试以下方法:

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

XML已被解析。你有什么问题?提取必要的信息?那会是什么信息呢?那么它被解析了吗?是的,我想提取必要的信息。这是我用来获取信息$x=file\u get\u contents($url)的代码$xml=simplexml\u load\u字符串($x);foreach($xml->categories->categories->items->product as$product){var_dump($product);}foreach($xml->categories->categories->items->offer as$offer){var_dump($offer);}?>我想展示每种产品的价格和图片等。据我所知,返回的内容应该是这样的:。我建议您仔细阅读,以便了解它的实际输出……这是我想做的部分工作,我应该能够了解其余部分。因此,如果我读对了,它会通读每种产品,并呼应名称、最高价格和最低价格?@Ryan:是的。可以以类似的方式访问每个子节点(例如:
$product->image[0]->sourceURL
)。注意:如果你觉得这个答案很有用,就投票给它(或者如果它解决了你的问题就接受它):)