开发者

PHP: How to detect/parse all TAGS in rss/feed XML?

I'm testing rss feed reader, from a sample. It uses:

$xmlDoc = new DOMDocument();
$xmlDoc->load($url);

$items = $xmlDoc->getElementsByTagName('item');
for ($i=0; $i < $items->length; $i++) {
    $item_title = $items->item($i)->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
    $item_link  = $items->item($i)->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;
    $item_desc  = $item开发者_C百科s->item($i)->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;
}

When i check the XML, it has another Tags like:

  • Date
  • Image Link

How can i call these all other tags? Because i can't call myself.

For example, for the 'date', i can't use getElementsByTagName('date').

It showing error: Fatal error: Call to a member function item() on a non-object

  • So, is there fixed names for tags?
  • If so, what are these?
  • (or) How can i know/ parse/ detact/ extract all available tags inside XML?


You can access each node recursively and you can choose from which one to get your data by checking if the tag name matches your desired one :

$indent = 0;
$tab = 4;
function indent($indent){
    $r = "";
    for($i=0;$i<$indent;$i++)
        $r .= " ";
    return $r;
}
function parseNode($node){
    global $indent,$tab;
    if(!$node->hasChildNodes())
        return;
    $indent += $tab;
    // if($note->tagName == "item") do something special
    echo indent($indent)."<".$note->nodeName.">";
    foreach ($node->childNodes as $c) 
        parseNode($c);

    echo indent($indent)."</".$note->nodeName.">";
    $indent -= $tab;
}
$xmlDoc = new DOMDocument();
$xmlDoc->load($url);
parseNode($xmlDoc);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜