parseItemsFromXMLString method
Implementation
List<AppcastItem>? parseItemsFromXMLString(String xmlString) {
items = null;
if (xmlString.isEmpty) {
return null;
}
try {
// Parse the XML
final document = XmlDocument.parse(xmlString);
// Ensure the root element is valid
document.rootElement;
var localItems = <AppcastItem>[];
// look for all item elements in the rss/channel
document.findAllElements('item').forEach((XmlElement itemElement) {
String? title;
String? itemDescription;
String? dateString;
String? fileURL;
String? maximumSystemVersion;
String? minimumSystemVersion;
String? osString;
String? releaseNotesLink;
final tags = <String>[];
String? newVersion;
String? itemVersion;
String? enclosureVersion;
itemElement.children.forEach((XmlNode childNode) {
if (childNode is XmlElement) {
final name = childNode.name.toString();
if (name == AppcastConstants.ElementTitle) {
title = childNode.text;
} else if (name == AppcastConstants.ElementDescription) {
itemDescription = childNode.text;
} else if (name == AppcastConstants.ElementEnclosure) {
childNode.attributes.forEach((XmlAttribute attribute) {
if (attribute.name.toString() ==
AppcastConstants.AttributeVersion) {
enclosureVersion = attribute.value;
} else if (attribute.name.toString() ==
AppcastConstants.AttributeOsType) {
osString = attribute.value;
} else if (attribute.name.toString() ==
AppcastConstants.AttributeURL) {
fileURL = attribute.value;
}
});
} else if (name == AppcastConstants.ElementMaximumSystemVersion) {
maximumSystemVersion = childNode.text;
} else if (name == AppcastConstants.ElementMinimumSystemVersion) {
minimumSystemVersion = childNode.text;
} else if (name == AppcastConstants.ElementPubDate) {
dateString = childNode.text;
} else if (name == AppcastConstants.ElementReleaseNotesLink) {
releaseNotesLink = childNode.text;
} else if (name == AppcastConstants.ElementTags) {
childNode.children.forEach((XmlNode tagChildNode) {
if (tagChildNode is XmlElement) {
final tagName = tagChildNode.name.toString();
tags.add(tagName);
}
});
} else if (name == AppcastConstants.AttributeVersion) {
itemVersion = childNode.text;
}
}
});
if (itemVersion == null) {
newVersion = enclosureVersion;
} else {
newVersion = itemVersion;
}
// There must be a version
if (newVersion == null || newVersion.isEmpty) {
return;
}
final item = AppcastItem(
title: title,
itemDescription: itemDescription,
dateString: dateString,
maximumSystemVersion: maximumSystemVersion,
minimumSystemVersion: minimumSystemVersion,
osString: osString,
releaseNotesURL: releaseNotesLink,
tags: tags,
fileURL: fileURL,
versionString: newVersion,
);
localItems.add(item);
});
items = localItems;
} catch (e) {
print('upgrader: parseItemsFromXMLString exception: $e');
}
return items;
}