RssFeed.parse constructor
RssFeed.parse(
- String xmlString
Parse constructor for the RssFeed class, used when 'parsing' a feed
Implementation
factory RssFeed.parse(String xmlString) {
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?.text,
author: channelElement.findElements('author').firstOrNull?.text,
description: channelElement.findElements('description').firstOrNull?.text,
link: channelElement.findElements('link').firstOrNull?.text,
items: (rdf ?? channelElement)
.findElements('item')
.map(RssItem.parse)
.toList(),
image: (rdf ?? channelElement)
.findElements('image')
.map(RssImage.parse)
.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.text)
.toList() ??
[],
skipHours: channelElement
.findElements('skipHours')
.firstOrNull
?.findAllElements('hour')
.map((e) => int.tryParse(e.text) ?? 0)
.toList() ??
[],
lastBuildDate:
channelElement.findElements('lastBuildDate').firstOrNull?.text,
language: channelElement.findElements('language').firstOrNull?.text,
generator: channelElement.findElements('generator').firstOrNull?.text,
copyright: channelElement.findElements('copyright').firstOrNull?.text,
docs: channelElement.findElements('docs').firstOrNull?.text,
managingEditor:
channelElement.findElements('managingEditor').firstOrNull?.text,
rating: channelElement.findElements('rating').firstOrNull?.text,
webMaster: channelElement.findElements('webMaster').firstOrNull?.text,
ttl: int.tryParse(
channelElement.findElements('ttl').firstOrNull?.text ?? '0',
),
dc: DublinCore.parse(channelElement),
itunes: Itunes.parse(channelElement),
syndication: Syndication.parse(channelElement),
);
}