xml_parser 0.1.0+3

  • Readme
  • Changelog
  • Example
  • Installing
  • 56

xml_parser #

An unopinionated XML parser that can read, traverse, and write XML documents.

Documentation (API Reference): https://pub.dev/documentation/xml_parser/latest/

Usage #

import 'package:xml_parser/xml_parser.dart';

Parsing #

Documents #

XML documents can be parsed from a string with the [XmlDocument.fromString] method, or from a URI with the [XmlDocument.fromUri] method.

String xmlString = '''
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE root SYSTEM "rootDtd" "dtd/root.dtd">
<root>
    <element>Child 1</element>
    <element>Child 2</element>
    <element>Child 3</element>
</root>
''';

XmlDocument xmlDocument = XmlDocument.fromString(xmlString);

print(xmlDocument.xmlDeclaration.version); // 1.0
print(xmlDocument.xmlDeclaration.encoding); // UTF-8
print(xmlDocument.xmlDeclaration.standalone); // true

print(xmlDocument.doctype.element); // root
print(xmlDocument.doctype.externalDtdUri); // dtd/root.dtd

print(xmlDocument.root.children.length); // 3
// Parse this package's pub.dev page as a XML document.
XmlDocument xmlDocument = await
    XmlDocument.fromUri('http://pub.dev/packages/xml_parser');

// Get the first `h2` element with a class of `title`.
XmlElement title = xmlDocument
    .getElementWhere(
       XmlElement(
        name: 'h2',
        attributes: [XmlAttribute('class', 'title')],
      ),
    );

print(title.text); // xml_parser 0.1.0

Note: HTML documents, as long as their empty tags are self-closing, can be parsed as XML. See the example for a more in-depth look at parsing this page: https://pub.dev/packages/xml_parser#-example-tab-

Extracting Nodes #

For many use cases there isn't a need to parse the entire XML document. In cases where you know the structure of the document and where the data you need to retrieve can be found you can extract just the nodes you need without parsing any more of the document than necessary.

Each XML node class has the static methods [fromString] and [parseString]. [fromString] will parse the inputted string and return the first node found of its type, while [parseString] will return a list of every node of that type found. These are methods are explained in more detail for each of their respective nodes below.

The base [XmlNode] class also has the [fromString] and [parseString] static methods with an option, [returnNodesOfType], for retrieving nodes of multiple types.

/// Returns all XML declarations, DOCTYPE declarations, and elements
/// found within [xmlString], ignoring all other node types.
///
/// Only the highest level nodes will be returned. If any of the returned
/// nodes have children, all children will be parsed regardless of type.
List<XmlNode> nodes = XmlNode.parseString(
  xmlString,
  returnNodesOfType: <Type>[XmlDeclaration, XmlDoctype, XmlElement],
);

Navigating Documents #

[XmlDocument]s and every type of node that can have children store their children in lists of [XmlNode]s.

Because XML nodes have varying properties, the only [toString] and [toFormattedString] methods are available every node (more on those at the bottom.) As such, all other getters and methods are specific to each node.

To access the properties of a node, test its type with the is operator:

String xmlString = '''
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE root SYSTEM "rootDtd" "dtd/root.dtd">
<root>
    <element>Child 1</element>
    <element>Child 2</element>
    <element>Child 3</element>
</root>
''';

XmlDocument document = XmlDocument.fromString(xmlString);

for (XmlNode child in document.children) {
  if (child is XmlDeclaration) {
    print(child.version); // 1.0
  } else if (child is XmlDoctype) {
    print(child.externalDtdName); // rootDtd
  } else if (child is XmlElement) {
    print(child.name); // root
  }
}

For convenience, all nodes that have a [children] property ([XmlDocument], [XmlElement], and [XmlConditional]), have the following getters and methods for retrieving nested [XmlElement]s.

Note: Check the API reference for more detailed information regarding each method's parameters.

/// Returns the first [XmlElement] found in [children].
XmlElement get firstChild;

/// Returns the nth [XmlElement] found in [children].
XmlElement nthChild(int index);

/// Returns the last [XmlElement] found in [children].
XmlElement get lastChild;

/// Returns the first direct child named [elementName].
XmlElement getChild(String elementName);

/// Returns the nth direct child named [elementName].
XmlElement getNthChild(int index, String elementName);

/// Returns the last direct child named [elementName].
XmlElement getLastChild(String elementName);

/// Returns all direct children named [elementName] between [start] and [stop].
List<XmlElement> getChildren(String elementName, {int start = 0, int stop});

/// Returns the first direct child with properties matching those specified.
XmlElement getChildWhere({
  String name,
  String id,
  List<String> attributeNames,
  List<XmlAttribute> attributes,
  bool matchAllAttributes = false,
  bool attributesMustBeIdentical = false,
  List<XmlNode> children,
  bool matchAllChildren = false,
  bool childrenMustBeIdentical = false,
});

/// Returns the nth direct child with properties matching those specified.
XmlElement getNthChildWhere(
  int index, {
  String name,
  String id,
  List<String> attributeNames,
  List<XmlAttribute> attributes,
  bool matchAllAttributes = false,
  bool attributesMustBeIdentical = false,
  List<XmlNode> children,
  bool matchAllChildren = false,
  bool childrenMustBeIdentical = false,
})

/// Returns the last direct child with properties matching those specified.
XmlElement getLastChildWhere({
  String name,
  String id,
  List<String> attributeNames,
  List<XmlAttribute> attributes,
  bool matchAllAttributes = false,
  bool attributesMustBeIdentical = false,
  List<XmlNode> children,
  bool matchAllChildren = false,
  bool childrenMustBeIdentical = false,
});

/// Returns all direct children with properties matching those specified.
List<XmlElement> getChildrenWhere({
  String name,
  String id,
  List<String> attributeNames,
  List<XmlAttribute> attributes,
  bool matchAllAttributes = false,
  bool attributesMustBeIdentical = false,
  List<XmlNode> children,
  bool matchAllChildren = false,
  bool childrenMustBeIdentical = false,
  bool ignoreNestedMatches = true,
  int start = 0,
  int stop,
});

Each of the above [getChild] methods also have a corresponding [getElement] method that recursively checks all elements and their children, all the way down the tree, and returns any matching elements.

/// Recursively checks all elements within the node tree and
/// returns the first element found named [elementName].
XmlElement getElement(String elementName);

/// Recursively checks all elements within the node tree and
/// returns the nth element found named [elementName].
XmlElement getNthElement(
  int index,
  String elementName, {
  bool ignoreNestedMatches = true
});

/// Recursively checks all elements within the node tree and
/// returns the last element found named [elementName].
XmlElement getLastElement(String elementName);

/// Recursively checks all elements within the node tree and returns
/// any elements found named [elementName] between [start] and [stop].
List<XmlElement> getElements(
  String elementName, {
  bool ignoreNestedMatches = true,
  int start = 0,
  int stop,
});

/// Recursively checks all elements within the node tree and returns
/// the first element found with properties matching those specified.
XmlElement getElementWhere({
  String name,
  String id,
  List<String> attributeNames,
  List<XmlAttribute> attributes,
  bool matchAllAttributes = false,
  bool attributesMustBeIdentical = false,
  List<XmlNode> children,
  bool matchAllChildren = false,
  bool childrenMustBeIdentical = false,
});

/// Recursively checks all elements within the node tree and returns
/// the nth element found with properties matching those specified.
XmlElement getNthElementWhere(
  int index, {
  String name,
  String id,
  List<String> attributeNames,
  List<XmlAttribute> attributes,
  bool matchAllAttributes = false,
  bool attributesMustBeIdentical = false,
  List<XmlNode> children,
  bool matchAllChildren = false,
  bool childrenMustBeIdentical = false,
});

/// Recursively checks all elements within the node tree and returns
/// the last element found with properties matching those specified.
XmlElement getLastElementWhere({
  String name,
  String id,
  List<String> attributeNames,
  List<XmlAttribute> attributes,
  bool matchAllAttributes = false,
  bool attributesMustBeIdentical = false,
  List<XmlNode> children,
  bool matchAllChildren = false,
  bool childrenMustBeIdentical = false,
});

/// Recursively checks all elements within the node tree and returns
/// any elements found with properties matching those specified.
List<XmlElement> getElementsWhere({
  String name,
  String id,
  List<String> attributeNames,
  List<XmlAttribute> attributes,
  bool matchAllAttributes = false,
  bool attributesMustBeIdentical = false,
  List<XmlNode> children,
  bool matchAllChildren = false,
  bool childrenMustBeIdentical = false,
  bool ignoreNestedMatches = true,
  int start = 0,
  int stop,
});

The following methods also exist for testing if a child exists:

/// Returns `true` if [children] isn't `null` or empty.
bool get hasChildren;

/// Returns `true` if this element contains a direct child named [elementName].
bool hasChild(String elementName);

/// Returns `true` if this element contains a direct child with
/// properties matching those specified.
bool hasChildWhere({
  String name,
  String id,
  List<String> attributeNames,
  List<XmlAttribute> attributes,
  bool matchAllAttributes = false,
  bool attributesMustBeIdentical = false,
  List<XmlNode> children,
  bool matchAllChildren = false,
  bool childrenMustBeIdentical = false,
});

/// Recursively checks all nested elements and returns `true` if one
/// is found named [elementName].
bool hasElement(String elementName);

/// Recursively checks all nested elements and returns `true` if one
/// is found with properties matching those specified.
bool hasElementWhere({
  String name,
  String id,
  List<String> attributeNames,
  List<XmlAttribute> attributes,
  bool matchAllAttributes = false,
  bool attributesMustBeIdentical = false,
  List<XmlNode> children,
  bool matchAllChildren = false,
  bool childrenMustBeIdentical = false,
});

Document Structure #

Documents are parsed as [XmlDocument] nodes. [XmlDocument]s have a single property, [children], a list of [XmlNode]s.

A document's XML and DOCTYPE declarations are parsed as [XmlDeclaration] and [XmlDoctype] nodes respectively, and can be referenced with the [xmlDeclaration] and [doctype] getters.

The document's root element is parsed as a [XmlElement] and can be referenced with the [root] getter.

Note: Valid XML documents only have a single XML declaration, DOCTYPE declaration and root element. While [XmlDocument] doesn't limit [children] to containing a single instance of each of these nodes, however only the first found node of each type will be returned with their respective getters.

XML Declarations #

XML declarations are parsed as [XmlDeclaration] nodes.

The [XmlDeclaration] node has 3 parameters: [version], [encoding], and [standalone]. All values are stored as [String]s, only [version] is required.

[XmlDeclaration]s can be parsed with the [XmlDeclaration.fromString] method, or every instance of a XML declaration found within a string, parsed, and extracted with the [XmlDeclaration.parseString] method.

String xmlDeclarationString =
    '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';

XmlDeclaration xmlDeclaration =
    XmlDeclaration.fromString(xmlDeclarationString);

print(xmlDeclaration.version); // 1.0
print(xmlDeclaration.encoding); // UTF-8
print(xmlDeclaration.standalone) // true

Note: Valid XML documents should only contain a single XML declaration as the first root-level node, but are allowed to be contained anywhere by the parser.

DOCTYPE Declarations #

DOCTYPE declarations are parsed as [XmlDoctype] nodes.

Note: Valid XML documents should only contain DOCTYPE declarations as a root-level node, but are allowed to be contained anywhere by the parser.

[XmlDoctype]s with external DTD values, and a fully qualified URI linking to them, can be returned as a new [XmlDoctype] node containing their external DTD nodes, by calling the [loadExternalDtd] method. null will be returned if the URI couldn't be reached. All XML nodes found at the URI will be retrieved, not just the valid DTD nodes.

XmlDoctype doctype = XmlDoctype.fromString(
    '<!DOCTYPE root PUBLIC "https://test.com/root.dtd">');

doctype = await doctype.loadExternalDtd();

print(doctype.externalDtd); // [<!ELEMENT author (#PCDATA)>, <!ELEMENT text (#PCDATA)>]

Note: If the [XmlDoctype] node is contained within a [XmlDocument], [XmlDocument]'s [loadExternalDtd] method can be called instead. All [XmlDoctype] and [XmlEntity] nodes will be checked for external DTD declarations and attempt to load each one.

Elements #

XmlElements are parsed as [XmlElement] nodes.

[XmlElement] has 3 parameters, [name], [attributes], and [children].

[children] can be referenced or traversed as described above.

[attributes] stores a list of [XmlAttribute]s, which contain a name and a value. Valid XML documents may not posess attributes without a value but, should the parser identify any attributes without a value, they will be parsed with a null value, and written by the [toString] and [toFormattedString] methods with an empty value.

[XmlElement] has several methods related to the [attributes] list. Attribute names are not case sensitive, but the values are.

/// Returns true if this element has an attribute named [attributeName].
bool hasAttribute(String attributeName);

/// Returns true if this element has an attribute named
/// [attributeName] and a value of [attributeValue].
bool hasAttributeWhere(String attributeName, String attributeValue);

/// Returns the value of the first attribute named [attributeName].
String getAttribute(String attributeName);

An element's ID can be retrieved with the getter [id], which will find the first attribute named [id], if it exists, and return its value.

All [XmlElement]s without any children will be considered an empty element, and will written with a self-closing tag by the [toString] and [toFormattedString] methods.

By default, [XmlElement]'s [parseString] method will only return top-level elements. Its [global] option can be set to true to return all elements, nested or not.

[parseString] has four options for filtering the returned elements. [returnElementsNamed], [returnElementsWithId], [returnElementsWithAttributeNamed], and [returnElementsWithAttribute].

[parseString] and all get methods that return [List]s have [start] and [stop] parameters to limit the elements returned to only the indexes found that fall on or after [start] and before [stop], which may be null. Indexes are counted from 0.

String xmlString = '''
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE root SYSTEM "rootDtd" "dtd/root.dtd">
<root>
    <element>Child 1</element>
    <element>Child 2</element>
    <element>Child 3</element>
    <element>Child 4</element>
    <element>Child 5</element>
    <element>Child 6</element>
</root>
''';

List<XmlElement> elements = XmlElement.parseString(
  xmlString,
  returnElementsNamed: ['element'],
  start: 2,
  stop: 4,
);

print(elements); // [<element>Child 3</element>, <element>Child 4</element>, <element>Child 5</element>]

Plain Text Values #

Plain text values are parsed as [XmlText] nodes. [XmlText] can be instanced directly, or parsed with the [XmlText.fromString] method, which has options to parse for character entities and trim whitespace.

String text = 'The `&amp;`, `&lt;`, and `&gt;` characters are reserved in XML.';

print(XmlText(text)); // The `&amp;`, `&lt;`, and `&gt;` characters are reserved in XML.

print(XmlText.fromString(text)); // The `&`, `<`, and `>` characters are reserved in XML.

The [toString] and [toFormattedString] methods have options to encode characters as character entities. By default, only the &, <, and > characters will be encoded. [XmlNode]s with their [isMarkup] parameter set to true, will never have their characters encoded even if the [encodeCharacterEntities] option is set to true, (it is by default.)

String text = '<text>Just some text.</text>';

print(XmlText(text)); // &lt;text&gt;Just some text.&lt;/text&gt;

print(XmlText(text).toString(encodeCharacterEntities: false)); // <text>Just some text.</text>

print(XmlText(text, isMarkup: true)); // <text>Just some text.</text>

XML nodes that can't be identified while parsing are also stored as [XmlText] nodes with the [isMarkup] parameter set to true. This way, when writing the document unidentifiable nodes will always be returned exactly as they were found.

Extracting Text:

Properly nested plain text values can be extracted from a XML string with the [parseString] method.

String xmlString = '''
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE root SYSTEM "rootDtd" "dtd/root.dtd">
<root>
    <element>Child 1</element>
    <element>Child 2</element>
    <text>Child 3</text>
</root>
''';

print(XmlText.parseString(xmlString)); // [Child 1, Child 2, Child 3]

[XmlElement]'s [text] getter will retrieve all top-level [XmlText] nodes from [children] and return their joined values as a string.

String xmlString = '''
<element>
  This text will
  <child>not be joined with this text.</child>
  be joined with this text.
</element>
''';

XmlElement element = XmlElement.fromString(xmlString);

print(element.text); // This text will be joined with this text.

print(element.firstChild.text); // not be joined with this text.

CDATA Sections #

CDATA sections are parsed as [XmlCdata] nodes.

[XmlCdata] only contains a single parameter, [value], a string which must not null, but may be empty.

[XmlCdata]'s value is left unmodified by the parser, with the exception of unnecessary whitespace if the [trimWhtiespace] option is true.

NOTE: By default CDATA sections are captured by the parser as [XmlText] nodes, allowing CDATA sections to be returned as text with the [XmlElement.text] getter. To parse CDATA as an [XmlCdata] node, you can set the [parseCdataAsText] option to false on the [toString] and [parseString] methods that support it.

Comments #

Comments are parsed as [XmlComment] nodes.

By default, the parsing methods on [XmlDocument] and the nodes that can contain children will remove comments, rather than parsing them. The parsing methods that support it, have a [parseComments] option, if you'd like comments to be captured, parsed, and instanced as a node.

Note: Valid XML documents don't allow comments within element tags or other node delimiters, outside of values that contain nested nodes. The parser removes comments found within nodes that don't support them, they can not be captured by any parsing methods besides [XmlComment]'s own [fromString] and [parseString] methods.

Conditional Sections #

Conditional sections are parsed as [XmlConditional] nodes. They have two parameters, [condition] and [children].

Because conditionals can accept an entity for the condition, and the parser doesn't decode XML entities (it has an option to parse for HTML, hex, and ascii entities,) [condition] is stored as a [String]. For convienence, the [XmlConditional] class has 2 static methods, [include] and [ignore] for building [XmlConditional]s with the [condition] set to INCLUDE and IGNORE, respectively.

ENTITY Declarations #

XML entities and DTD parameter entities are both parsed as [XmlEntity], with the latter having the [isParameter] option set to true. [XmlEntity] has 6 other parameters, [name], [value], [isSystem], [isPublic], [externalEntities], and [ndata].

[name] and [value] are accepted as positional parameters and must be provided.

[isSystem] and [isPublic] define whether SYSTEM or PUBLIC flags were declared on the entity. Only one may be true.

[externalEntities] isn't captured by the parser when parsing an entity declaration, but can be loaded with the [loadExternalEntities] method, which returns a new [XmlEntity] instance with [externalEntities] set, assuming the URI stored in [value] can be reached, otherwise null is returned. If [XmlDocument]'s [loadExternalEntities] method is called, any [XmlEntity]s with external entity declarations will have their external entities loaded and replaced within the document with the new [XmlEntity] instance.

[ndata] is identified by the parser by the NDATA flag. Any content found after the flag is captured as the [ndata] value.

NOTATION Declarations #

Notation declarations are parsed as [XmlNotation] nodes. [XmlNotation] has 5 parameters, [name], [isSystem], [isPublic], [publicId], and [uri].

Processing Instructions #

Processing instructions are parsed as [XmlProcessingInstruction] nodes. [XmlProcessingInstruction] has 2 parameters, [target] and [content].

As processing instructions vary by application, their [content] is left unmodified, with the exception of any unnecessary whitespace if the [trimWhitespace] option is true.

ELEMENT Type Declarations #

DTD element type declarations are parsed as [XmlEtd] nodes.

As the parser doesn't validate XML code, [XmlEtd]s are captured in the same way [XmlProcessingInstruction]s are, but with [name] and [children] parameters. No distinction is made by the parser between parent and child element type declarations, and their [children] values are left unparsed.

ATTLIST Declarations #

DTD ATTLIST declarations are parsed as [XmlAttlist] nodes. [XmlAttlist]s have 5 parameters: [element], [attribute], [type], [identifier], and [defaultValue].

[element], [attribute], and [type] are required, or parsing will fail, and the node will be parsed as text. [identifier] is optional and is parsed as an enum value, [XmlAttlistIdentifier]. [defaultValue] should only be provided with [XmlAttlistIdentifier.FIXED] values.

Building Documents/Nodes #

xml_parser takes a hands-off approach to building documents and nodes. No building methods are provided. Instead, [XmlDocument], and all nodes that can contain children, store their nested nodes in modifiable [List]s.

Because every node is constant, to modify them, you must overwrite their reference within the list that contains them.

XmlDocument xmlDocument = XmlDocument([
  XmlDeclaration(version: '1.0', encoding: 'UTF-8'),
  XmlElement(name: 'root', children: [
    XmlElement(name: 'child', children: [XmlText('Child 1')]),
    XmlElement(name: 'child', children: [XmlText('Child 2')]),
    XmlElement(name: 'child', children: [XmlText('Child 3')]),
    XmlElement(name: 'child', children: [XmlText('Child 4')]),
  ]),
]);

xmlDocument.root.children[2] = XmlComment('Child 3');

print(xmlDocument.writeToFile('document.xml'));

The above code would write document.xml to the project root as:

<?xml version="1.0" encoding="UTF-8" ?>
<root>
    <child>Child 1</child>
    <child>Child 2</child>
    <!--Child 3-->
    <child>Child 4</child>
</root>

Writing Documents/Nodes #

[XmlDocument], and every node, have [toString] and [toFormattedString] methods.

  • [toString] will output unformatted XML code without any line breaks.

  • [toFormattedString] will format the XML code, breaking the line with each node/element, indenting each line as necessary. (Note: toFormattedString is rather rudimentary at the moment and I'd like to add additional formatting options to it at some point.)

[0.1.0] - August 27, 2019

  • Initial release.

example/main.dart

import 'package:xml_parser/xml_parser.dart';

// Calling `main('xml_parser')` would print:
// xml_parser 0.1.0
// Description: An unopinionated XML parser that can read, traverse, and write XML documents.
// Repo: https://github.com/james-alex/xml_parser
// Issues: https://github.com/james-alex/xml_parser/issues
// Documentation: https://pub.dev/documentation/xml_parser/latest/
// Author: James Alexander <j.alex@email.com>
// Uploaded by: [j.alex@email.com]
// License: BSD (https://github.com/james-alex/xml_parser/blob/master/LICENSE)
// Dependencies: [html_character_entities, http, recursive_regex]
// 0 packages depend on xml_parser.
// Popularity: 0
// Health: 100
// Maintenance: 100
// Overall: 50

/// Retreives and prints a package's details and analytics data from pub.dev.
///
/// Note: To call [main] in a test, use `await`, otherwise it won't print anything.
void main(String package) async {
  assert(package != null && package.isNotEmpty);

  // Get the package page's document and parse it.
  final XmlDocument packagePage =
      await XmlDocument.fromUri('https://pub.dev/packages/$package');

  if (packagePage == null) {
    throw ('Failed to load page.');
  }

  // Get the title of the page and check if the package exists.
  final String pageTitle = packagePage.getElement('title').text;

  if (pageTitle.startsWith('Search results')) {
    throw ('$package doesn\'t exist.');
  }

  // Get the package title from the page.
  final String title = packagePage.getElementWhere(
    name: 'h2',
    attributes: [XmlAttribute('class', 'title')],
  ).text;

  print(title);

  // Get the sidebar element.
  final XmlElement sidebar = packagePage.getElementWhere(
    name: 'aside',
    attributes: [XmlAttribute('class', 'sidebar sidebar-content')],
  );

  // Get the description text from the sidebar.
  final String description = sidebar.getChild('p').text?.trim();

  if (description != null && description.isNotEmpty) print(description);

  // Get the links from the sidebar.
  final XmlElement links = sidebar.getNthChild(1, 'p');

  final String repository = links.getChild('a').getAttribute('href');

  print('Repo: $repository');

  final String issues = links.getNthChild(1, 'a').getAttribute('href');

  print('Issues: $issues');

  final String documentation =
      'https://pub.dev' + links.getNthChild(2, 'a').getAttribute('href');

  print('Documentation $documentation');

  final XmlElement author = sidebar.getElementWhere(
    name: 'span',
    attributes: [XmlAttribute('class', 'author')],
  );

  final String authorName = author.text;

  final String authorEmail =
      author.getChild('a').getAttribute('href').substring(7);

  print('Author: $authorName <$authorEmail>');

  // Get the list of uploaders from the sidebar.
  final List<String> uploaders = List<String>();

  final List<XmlElement> uploaderEmails = sidebar.getElementsWhere(
    name: 'span',
    attributes: [XmlAttribute('class', 'author')],
    start: 1,
  );

  uploaderEmails.forEach((XmlElement uploader) {
    uploaders.add(uploader
        .getChild('a')
        .getAttribute('href')
        .replaceFirst('mailto:', ''));
  });

  print('Uploaded by: $uploaders');

  // The index of the next optional child (license and dependencies).
  int nextChild = (description != null) ? 3 : 2;

  // Get the license from the sidebar, if one exists.
  if (sidebar.hasElementWhere(
    name: 'h3',
    attributes: [XmlAttribute('class', 'title')],
    children: [XmlText('License')],
  )) {
    final XmlElement license = sidebar.getNthElement(nextChild, 'p');

    final String licenseType = license.text.split('(').first.trim();

    final String licenseUrl = license.getChild('a')?.getAttribute('href');

    print('License: $licenseType${licenseUrl != null ? ' ($licenseUrl)' : ''}');

    nextChild++;
  }

  // Get the dependencies from the sidebar, if any exist.
  if (sidebar.hasElementWhere(
    name: 'h3',
    attributes: [XmlAttribute('class', 'title')],
    children: [XmlText('Dependencies')],
  )) {
    final List<String> dependencies = sidebar
        .getNthElement(nextChild, 'p')
        .getElements('a')
        .map((XmlElement link) => link.text)
        .toList();

    print('Dependencies: $dependencies');
  }

  // Get the number of packages that depend on this package.
  final int numberOfDependants = await _getNumberOfDependants(package);

  print('$numberOfDependants packages depend on $package.');

  // Get the divs containing the scores.
  final List<XmlElement> scores =
      packagePage.getElementWhere(id: 'scores-table').getElementsWhere(
    name: 'div',
    attributes: [XmlAttribute('class', 'score-percent')],
  );

  // Popularity Score
  final int popularity = int.parse(scores[0].text);

  print('Popularity: $popularity');

  // Health Score
  final int health = int.parse(scores[1].text);

  print('Health: $health');

  // Maintenance Score
  final int maintenance = int.parse(scores[2].text);

  print('Maintenance: $maintenance');

  // Overall Score
  final int overall = int.parse(scores[3].text);

  print('Overall: $overall');
}

/// Gets the number of results from the dependency search page.
Future<int> _getNumberOfDependants(String package) async {
  final XmlDocument searchPage = await XmlDocument.fromUri(
      'https://pub.dev/packages?q=dependency%3A$package');

  if (searchPage == null) {
    throw ('Failed to load dependency seach results.');
  }

  final String numberOfResults = searchPage
      .getElementWhere(
        name: 'p',
        attributes: [XmlAttribute('class', 'package-count')],
      )
      .getChild('span')
      .text;

  return int.tryParse(numberOfResults);
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  xml_parser: ^0.1.0+3

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:xml_parser/xml_parser.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
15
Health:
Code health derived from static analysis. [more]
94
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
56
Learn more about scoring.

We analyzed this package on Dec 7, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:xml_parser/xml_parser.dart.

Health suggestions

Fix lib/src/nodes/dtd/xml_attlist.dart. (-1.99 points)

Analysis of lib/src/nodes/dtd/xml_attlist.dart reported 4 hints:

line 34 col 35: Use isNotEmpty instead of length

line 35 col 37: Use isNotEmpty instead of length

line 36 col 32: Use isNotEmpty instead of length

line 37 col 40: Use isNotEmpty instead of length

Fix lib/src/nodes/dtd/xml_etd.dart. (-0.50 points)

Analysis of lib/src/nodes/dtd/xml_etd.dart reported 1 hint:

line 17 col 32: Use isNotEmpty instead of length

Fix lib/src/nodes/xml_attribute.dart. (-0.50 points)

Analysis of lib/src/nodes/xml_attribute.dart reported 1 hint:

line 13 col 30: Use isNotEmpty instead of length

Fix additional 7 files with analysis or formatting issues. (-3 points)

Additional issues in the following files:

  • lib/src/nodes/xml_conditional.dart (1 hint)
  • lib/src/nodes/xml_doctype.dart (1 hint)
  • lib/src/nodes/xml_element.dart (1 hint)
  • lib/src/nodes/xml_entity.dart (1 hint)
  • lib/src/nodes/xml_notation.dart (1 hint)
  • lib/src/nodes/xml_processing_instruction.dart (1 hint)
  • lib/src/xml_node.dart (Run dartfmt to format lib/src/xml_node.dart.)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
html_character_entities ^0.1.2 0.1.2+1
http ^0.12.0+2 0.12.0+2
meta ^1.1.7 1.1.8
recursive_regex ^0.1.3 0.1.3
Transitive dependencies
async 2.4.0
charcode 1.1.2
collection 1.14.12
http_parser 3.1.3
path 1.6.4
pedantic 1.9.0
source_span 1.5.5
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
Dev dependencies
test ^1.6.5