xml 2.3.1 copy "xml: ^2.3.1" to clipboard
xml: ^2.3.1 copied to clipboard

outdatedDart 1 only

A lightweight library for parsing, traversing, querying and building XML documents.

Dart XML #

Pub Package Build Status Coverage Status

Dart XML is a lightweight library for parsing, traversing, querying and building XML documents.

This library is open source, stable and well tested. Development happens on GitHub. Feel free to report issues or create a pull-request there. General questions are best asked on StackOverflow.

Up-to-date class documentation is created automatically with every new release.

Basic Usage #

Installation #

Add the dependency to your package's pubspec.yaml file:

dependencies:
  xml: ">=2.0.0 <3.0.0"

Then on the command line run:

$ pub get

To import the package into your Dart code write:

import 'package:xml/xml.dart';

Reading and Writing #

To read XML input use the top-level function parse(String input):

var bookshelfXml = '''<?xml version="1.0"?>
    <bookshelf>
      <book>
        <title lang="english">Growing a Language</title>
        <price>29.99</price>
      </book>
      <book>
        <title lang="english">Learning XML</title>
        <price>39.95</price>
      </book>
      <price>132.00</price>
    </bookshelf>''';
var document = parse(bookshelfXml);

The resulting object is an instance of XmlDocument. In case the document cannot be parsed, a ParserError is thrown.

To write back the parsed XML document simply call toString():

print(document.toString());

Traversing and Querying #

Accessors allow to access nodes in the XML tree:

  • attributes returns an iterable over the attributes of the current node.
  • children returns an iterable over the children of the current node.

There are various methods to traverse the XML tree along its axes:

  • preceding returns an iterable over nodes preceding the opening tag of the current node in document order.
  • descendants returns an iterable over the descendants of the current node in document order. This includes the attributes of the current node, its children, the grandchildren, and so on.
  • following the nodes following the closing tag of the current node in document order.
  • ancestors returns an iterable over the ancestor nodes of the current node, that is the parent, the grandparent, and so on. Note that this is the only iterable that traverses nodes in reverse document order.

For example, the descendants iterator could be used to extract all textual contents from an XML tree:

var textual = document.descendants
    .where((node) => node is XmlText && !node.text.trim().isEmpty)
    .join('\n');
print(textual);

Additionally, there are helpers to find elements with a specific tag:

  • findElements(String name) finds direct children of the current node with the provided tag name.
  • findAllElements(String name) finds direct and indirect children of the current node with the provided tag name.

For example, to find all the nodes with the

402
likes
0
pub points
99%
popularity

Publisher

verified publisherlukas-renggli.ch

A lightweight library for parsing, traversing, querying and building XML documents.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

petitparser

More

Packages that depend on xml