xml 2.0.0
xml: ^2.0.0 copied to clipboard
A lightweight library for parsing, traversing, and querying XML documents.
Dart XML #
Dart XML is a lightweight library for parsing, traversing, and querying XML documents.
The 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.
Continuous build results are available from Jenkins. An introductionary tutorial is part of the class documentation.
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 ParseError is thrown.
To write back the parsed XML document simply call toString():
print(document.toString());
Traversing and Querying #
Accessors allow to access all nodes in the XML tree:
attributesreturns an iterable over the attributes of the current node.childrenreturns an iterable over the direct children of the current node.
There are helpers to find elements with a specific tag:
findElements(String name)finds direct children of the current node with the provided tagname.findAllElements(String name)finds direct and indirect children of the current node with the provided tagname.
For example, to find all the nodes with the