开发者

using php to check if xml is atom or rss

开发者 https://www.devze.com 2023-02-02 11:12 出处:网络
i\'m writing a php code which has to determine if given xml is in \"atom\" or \"rss\" format. After observing atom and rss xml files, i decided classify xml based on a root element. If root element is

i'm writing a php code which has to determine if given xml is in "atom" or "rss" format. After observing atom and rss xml files, i decided classify xml based on a root element. If root element is "<feed" it's an at开发者_StackOverflow社区om xml. If it's "<rss" it's not an atom.

How can i perform that check using DOM? So far i have:

$dom = new DOMDocument();
$dom->loadXML($resp);
$feed = $dom->getElementsByTagName("feed");
if($feed != NULL)
echo 'it\'s a atom!';

but it's not working quite right....There's no errors, it just write "it's a atom" even if it isn't


Not sure if $resp is a string or a filepath, but here is what I might do.

$xml = simplexml_load_file($filepath);
$root_element_name = $xml->getName();
if ($root_element_name  == 'feed') {
    // is atom feed

} else if ($root_element_name  == 'rss') {
    // is rss feed

}

This will load the XML, and find the name of the root node. If the root node is named feed, it is atom, if the root node is rss, it is rss.


I'll wager that you can get that a hint easier. If you're looking for a root element name examine:

$dom->documentElement->tagName;

That isn't tested, but it should give you what you are looking for in a much cleaner and clearer way.


looks like $dom is a DOMDocument - in that case, what i would try to do is the following:

$dom->loadXML($resp);
if($dom->getElementsByTagName("feed")->length > 0 && $dom->getElementsByTagName("rss")->length <= 0){
  // atom feed
}else{
  // rss feed
}

(i never used DOMDocument, just read the documentation - i'm sorry if this is wrong)

EDIT:

just saw your additional code - wahts wron with it is checking for null. getElementsByTagName will always return a DOMNodeList (an empty one if no elements where found)


Rather than testing for null, check the number of items returned:

$dom = new DOMDocument();
$dom->loadXML($resp);
$feed = $dom->getElementsByTagName("feed");
if($feed->length != 0) {
    echo 'it\'s a atom!';
}


Depending on how/where from you are parsing data you can check the headers for application/atom+xml. Alternatively, you may want to check using strpos('http://www.w3.org/2005/Atom').

0

精彩评论

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

关注公众号