php解析后访问xml对象

时间:2012-01-22 04:43:01

标签: php xml xml-parsing

HI有人可以解释如何访问此xml提要的“项目”部分。这是文件的VAR_DUMP。我使用simplexml_load_string从google中提取Feed,但如何访问“item”内容

    object(SimpleXMLElement)[1]
  public '@attributes' => 
    array
      'version' => string '2.0' (length=3)
  public 'channel' => 
    object(SimpleXMLElement)[2]
      public 'generator' => string 'NFE/1.0' (length=7)
      public 'title' => string 'running barefoot - Google News' (length=30)
      public 'link' => string 'http://news.google.com/news?pz=1&ned=us&hl=en&q=running+barefoot' (length=64)
      public 'language' => string 'en' (length=2)
      public 'webMaster' => string 'news-feedback@google.com' (length=24)
      public 'copyright' => string '©2012 Google' (length=17)
      public 'pubDate' => string 'Sun, 22 Jan 2012 04:38:34 GMT' (length=29)
      public 'lastBuildDate' => string 'Sun, 22 Jan 2012 04:38:34 GMT' (length=29)
      public 'image' => 
        object(SimpleXMLElement)[3]
          public 'title' => string 'running barefoot - Google News' (length=30)
          public 'url' => string 'http://www.gstatic.com/news/img/logo/en_us/news.gif' (length=51)
          public 'link' => string 'http://news.google.com/news?pz=1&ned=us&hl=en&q=running+barefoot' (length=64)
      public 'item' => 
        array
          0 => 
            object(SimpleXMLElement)[4]
              ...
          1 => 
            object(SimpleXMLElement)[5]
              ...
          2 => 
            object(SimpleXMLElement)[6]
              ...
          3 => 
            object(SimpleXMLElement)[7]
              ...
          4 => 
            object(SimpleXMLElement)[8]
              ...
          5 => 
            object(SimpleXMLElement)[9]
              ...
          6 => 
            object(SimpleXMLElement)[10]
              ...
          7 => 
            object(SimpleXMLElement)[11]
              ...
          8 => 
            object(SimpleXMLElement)[12]
              ...
          9 => 
            object(SimpleXMLElement)[13]
              ...
      public 'description' => string 'Google News' (length=11)

2 个答案:

答案 0 :(得分:2)

看起来你可以像这样访问'item'成员变量:$yourObject->channel->item

答案 1 :(得分:1)

你问过之前有没有试过搜索?有很多教程如何解析RSS提要 - for example

foreach($yourObject->channel->item as $item)
{
  echo $item->title;
  echo $item->link;
}