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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With