2011-08-19 68 views
0

我想從XML獲得最新的評論。一個例子可以在這裏找到:http://dev.smoige.com/discussion.xml?DiscussionID=8。我設法得到LastCommentID,我們可以說是37,可以在這種格式<CommentID>37</CommentID>中找到,下面是這個:<Body>this is my damn comment!</Body>,我真的在這之後。我如何使用LastCommentID來選擇正確的CommentID並獲取它的正文?使用PHP獲取XML數據。卡住?

請幫忙。

$latestXml = 'http://dev.smoige.com/discussion.xml?DiscussionID='.$Discussion->DiscussionID; 

     if(!$xml=simplexml_load_file($latestXml)){ 
      trigger_error('Error reading XML file',E_USER_ERROR); 
     } 

    foreach($xml as $user){ 
     $last = $user->LastCommentID.' '; 

    } 

謝謝

回答

0

簡短的回答:XPath的。

這裏有一個很好的教程:

http://www.phpeveryday.com/articles/PHP-XML-Tutorial-P848.html

<?php 
    $xml = simplexml_load_file("books.xml") 
     or die("Error: Cannot create object"); 
    // we want to show just <title> nodes 
    foreach($xml->xpath('//title') as $title){ 
    echo $title. "<br />"; 
    } 
?> 
+0

感謝您的幫助的人。無法做任何事情,但我會繼續努力。 – Ciprian