Sunday, July 27, 2014

PHP for parsing XML

PHP for parsing XML


PHP for parsing XML

Posted: 26 Jul 2014 11:41 AM PDT




I am trying to parse XML and print a tag but I am getting error saying.

Fatal error: Call to undefined method DOMNodeList::getElementsByTagName()

Mã PHP:

<?
        $doc 
= new DOMDocument();
        
$doc->load('http://www.espncricinfo.com/rss/content/story/feeds/0.xml');
        
$item $doc -> getElementsByTagName('item');
        
$title $item->getElementsByTagName('title');
        
$titles $title->item(0)->nodeValue;
        echo 
"<h2>$titles</h2>"
        
?>

To work with rss feeds I recommend you just use SimpleXML:

Mã PHP:

function h($s) {
    return 
htmlspecialchars($sENT_QUOTES'utf-8');
}

$u 'http://www.espncricinfo.com/rss/content/story/feeds/0.xml';
$rss simplexml_load_file($u);
foreach (
$rss->channel->item as $item) {
    echo 
"<h2>",h($item->title),"</h2>\n";











----------
Nguồn www.yeuquangngai.net

No comments:

Post a Comment