toMap static method

Map toMap({
  1. required XmlElement node,
})

Takes an XmlElement and creates a map from the attributes and any children elements

Implementation

// TODO: Further describe this
static Map<dynamic, dynamic> toMap({required XmlElement node}) {
  Map<dynamic, dynamic> map = <dynamic, dynamic>{};

  try {
    //////////////////
    /* Remember Xml */
    //////////////////
    map["xml"] = node.toXmlString();

    ////////////////
    /* Attributes */
    ////////////////
    List<XmlAttribute> attributes = node.attributes;
    for (XmlAttribute attribute in attributes) {
      String value = attribute.value.trim();
      String name = attribute.name.toString();
      map[name] = value;
    }

    //////////////
    /* Elements */
    //////////////
    List<XmlNode> children = node.children;
    for (XmlNode child in children) {
      if (child is XmlElement) {
        XmlElement e = child;
        String? value = e.value?.trim();
        String name = e.name.toString();
        map[name] = hasChildElements(child)
            ? child.outerXml.toString().trim()
            : value;
      } else if (child is XmlText) {
        XmlText e = child;
        String value = e.value.trim();
        String name = node.localName;
        map[name] = value;
      } else if (child is XmlCDATA) {
        XmlCDATA e = child;
        String value = e.innerText.trim();
        String name = node.localName;
        map[name] = value;
      }
    }
  } catch (e) {
    Log().exception(e,
        caller: 'xml.dart => Map<dynamic,dynamic> toMap({XmlElement node})');
  }
  return map;
}