detectRssVersion static method

RssVersion detectRssVersion(
  1. String xmlString
)

Implementation

static RssVersion detectRssVersion(String xmlString) {
  final xmlDoc = xml.XmlDocument.parse(xmlString);
  final rdfRefs = xmlDoc.findAllElements('rdf:RDF');
  final rssRefs = xmlDoc.findAllElements('rss');
  final feedRefs = xmlDoc.findAllElements('feed');

  bool? ver = false;
  bool? xmlns = false;
  ver = rssRefs.isEmpty ? false : rssRefs.first.getAttribute('version')?.contains('2');
  xmlns = feedRefs.isEmpty ? false : feedRefs.first.getAttribute('xmlns')?.toLowerCase().contains('atom');

  if (rdfRefs.isNotEmpty) {
    return RssVersion.rss1;
  } else if (rssRefs.isNotEmpty && ver != null && ver) {
    return RssVersion.rss2;
  } else if (feedRefs.isNotEmpty && xmlns != null && xmlns) {
    return RssVersion.atom;
  }
  return RssVersion.unknown;
}