douwen7905 2013-12-21 17:30
浏览 41

获取对象中的数据 - SimpleXML

So I got a simple function that works, but I'm trying to evolve my experince wtih OOP and try to make sure I can use my code without having to edit everything.

Here is my simple function

$xmlfeed = file_get_contents('/forum/syndication.php?limit=3');

$xml = new SimpleXMLElement($xmlfeed);
$result = $xml->xpath('channel/item/title');

while(list( , $node) = each($result)) {
    echo $node;
}

Now so far I got to this point:

class ForumFeed {
    private function getXMLFeeds($feed = 'all'){
        /*
            Fetch the XML feeds
         */
        $globalFeedXML = file_get_contents('/forum/syndication.php?limit=3');
        $newsFeedXML = file_get_contents('/forum/syndication.php?fid=4&limit=3');

        /*
            Turn feed strings into actual objects
         */
        $globalFeed = new SimpleXMLElement($globalFeedXML);
        $newsFeed = new SimpleXMLElement($newsFeedXML);

            /*
                Return requested feed
             */
            if ($feed == 'news') {
                return $newsFeed;
            } else if ($feed == 'all') {
                return $globalFeed;
            } else {
                return false;
            }

    }
    public function formatFeeds($feed) {
        /*
            Format Feeds for displayable content..
            For now we're only interested in the titles of each feed
         */
        $getFeed = $this->getXMLFeeds($feed);

        return $getFeed->xpath('channel/item/title');
    }
}

$feeds = new ForumFeed();

However when trying to echo $feeds->formatFeeds('all'); it doesn't return anything. The results is blank.

What am I doing wrong?

var_dump($feeds->formatFeeds('all')); returns

array(3) {
  [0]=>
  object(SimpleXMLElement)#3 (0) {
  }
  [1]=>
  object(SimpleXMLElement)#4 (0) {
  }
  [2]=>
  object(SimpleXMLElement)#5 (0) {
  }
}
  • 写回答

1条回答 默认 最新

  • dongruidian3064 2013-12-21 17:36
    关注

    According to PHPs documentation SimpleXMLElement::xpath returns an array of SimpleXMLElements or false on error. Maybe var_dump($feeds->formatFeeds('all')); prints something you then can use to debug.

    Edit: The XPath query returns results, so probably there is a logical error in your query or the returned elements don't have content.

    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应