parseItemsFromXMLString method

List<AppCastItem>? parseItemsFromXMLString(
  1. String xmlString
)

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;

      for (var childNode in itemElement.children) {
        if (childNode is XmlElement) {
          final name = childNode.name.toString();
          if (name == AppCastConstants.ElementTitle) {
            title = childNode.innerText;
          } else if (name == AppCastConstants.ElementDescription) {
            itemDescription = childNode.innerText;
          } else if (name == AppCastConstants.ElementEnclosure) {
            for (var attribute in childNode.attributes) {
              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.innerText;
          } else if (name == AppCastConstants.ElementMinimumSystemVersion) {
            minimumSystemVersion = childNode.innerText;
          } else if (name == AppCastConstants.ElementPubDate) {
            dateString = childNode.innerText;
          } else if (name == AppCastConstants.ElementReleaseNotesLink) {
            releaseNotesLink = childNode.innerText;
          } else if (name == AppCastConstants.ElementTags) {
            for (var tagChildNode in childNode.children) {
              if (tagChildNode is XmlElement) {
                final tagName = tagChildNode.name.toString();
                tags.add(tagName);
              }
            }
          } else if (name == AppCastConstants.AttributeVersion) {
            itemVersion = childNode.innerText;
          }
        }
      }

      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) {
    if (kDebugMode) {
      print('hcUpgrade: parseItemsFromXMLString exception: $e');
    }
  }

  return items;
}