Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using php to check if xml is atom or rss

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 atom 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

like image 254
guest86 Avatar asked Nov 25 '25 06:11

guest86


2 Answers

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.

like image 130
dqhendricks Avatar answered Nov 27 '25 22:11

dqhendricks


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.

like image 23
cwallenpoole Avatar answered Nov 27 '25 21:11

cwallenpoole



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!