RssFeed.parse constructor

RssFeed.parse(
  1. String xmlString, [
  2. bool parseHtml = true
])

Parse constructor for the RssFeed class, used when 'parsing' a feed If parseHtml is true, HTML tags will be parsed from the feed

Implementation

factory RssFeed.parse(String xmlString, [bool parseHtml = true]) {
  final document = XmlDocument.parse(xmlString);
  final rss = document.findElements('rss').firstOrNull;
  final rdf = document.findElements('rdf:RDF').firstOrNull;
  if (rss == null && rdf == null) {
    throw ArgumentError('not a rss feed');
  }
  final channelElement = (rss ?? rdf)!.findElements('channel').firstOrNull;
  if (channelElement == null) {
    throw ArgumentError('channel not found');
  }
  return RssFeed(
    title: channelElement
        .findElements('title')
        .firstOrNull
        ?.parseText(parseHtml),
    author: channelElement.findElements('author').firstOrNull?.innerText,
    description: channelElement
        .findElements('description')
        .firstOrNull
        ?.parseText(parseHtml),
    link: channelElement.findElements('link').firstOrNull?.innerText,
    items: (rdf ?? channelElement)
        .findElements('item')
        .map((item) => RssItem.parse(item, parseHtml))
        .toList(),
    image: (rdf ?? channelElement)
        .findElements('image')
        .map((image) => RssImage.parse(image, parseHtml))
        .firstOrNull,
    cloud:
        channelElement.findElements('cloud').map(RssCloud.parse).firstOrNull,
    categories: channelElement
        .findElements('category')
        .map(RssCategory.parse)
        .toList(),
    skipDays: channelElement
            .findElements('skipDays')
            .firstOrNull
            ?.findAllElements('day')
            .map((e) => e.innerText)
            .toList() ??
        [],
    skipHours: channelElement
            .findElements('skipHours')
            .firstOrNull
            ?.findAllElements('hour')
            .map((e) => int.tryParse(e.innerText) ?? 0)
            .toList() ??
        [],
    lastBuildDate:
        channelElement.findElements('lastBuildDate').firstOrNull?.innerText,
    language: channelElement.findElements('language').firstOrNull?.innerText,
    generator:
        channelElement.findElements('generator').firstOrNull?.innerText,
    copyright:
        channelElement.findElements('copyright').firstOrNull?.innerText,
    docs: channelElement.findElements('docs').firstOrNull?.innerText,
    managingEditor:
        channelElement.findElements('managingEditor').firstOrNull?.innerText,
    rating: channelElement.findElements('rating').firstOrNull?.innerText,
    webMaster:
        channelElement.findElements('webMaster').firstOrNull?.innerText,
    ttl: int.tryParse(
      channelElement.findElements('ttl').firstOrNull?.innerText ?? '0',
    ),
    dc: DublinCore.parse(channelElement, parseHtml),
    itunes: Itunes.parse(channelElement, parseHtml),
    syndication: Syndication.parse(channelElement),
  );
}