Dart XML
last modified January 28, 2024
In this article we show how to work with XML data in Dart language. We read and build XML data.
Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. XML is often used for application configuration, data storage and exchange.
XML is similar to HTML, but does not have predefined tags; we can design our own tags.
In Dart, we use the package:xml/xml.dart
to work with XML.
$ dart pub add xml
We add the package to the project structure.
Dart XML simple example
In the first example, we read XML data from a string with
XmlDocument.parse
. The method returns an XmlDocument
for the given input string.
import 'package:xml/xml.dart'; void main() { var data = '''<?xml version="1.0" encoding="utf-8"?> <value>6</value>'''; var xdoc = XmlDocument.parse(data); print(xdoc); print('------------------'); print(xdoc.findAllElements('value').first); }
The example parses the XML data and prints the whole document to the terminal.
We also find the value
element and print it.
$ dart simple.dart <?xml version="1.0" encoding="utf-8"?> <value>6</value> ------------------ <value>6</value>
Dart read XML from file
In the next example, we read XML data from a file.
<?xml version="1.0" encoding="utf-8"?> <bookstore> <book genre='Science Fiction'> <title>Dune</title> <author>Frank Herbert</author> <price>8.99</price> </book> <book genre='Novel'> <title>Old Goriot</title> <author>Honoré de Balzac</author> <price>9.0</price> </book> </bookstore>
We have this books.xml
file.
import 'dart:io'; import 'package:xml/xml.dart'; void main() { var file = new File('books.xml'); String data = file.readAsStringSync(); var xdoc = XmlDocument.parse(data); print(xdoc); }
We import the dart:io
package and read the contents of the file
with the File's
readAsStringSync
method.
var xdoc = XmlDocument.parse(data);
We parse the data into an XML document with XmlDocument.parse
.
$ dart read_file.dart <?xml version="1.0" encoding="utf-8"?> <bookstore> <book genre='Science Fiction'> <title>Dune</title> <author>Frank Herbert</author> <price>8.99</price> </book> <book genre='Novel'> <title>Old Goriot</title> <author>Honoré de Balzac</author> <price>9.0</price> </book> </bookstore>
Dart XML where
The where
method returns a lazy iterable from the document which
satisfies the given predicate.
import 'dart:io'; import 'package:xml/xml.dart'; void main() async { var file = new File('books.xml'); String data = file.readAsStringSync(); var xdoc = XmlDocument.parse(data); xdoc.descendants .where((e) => e.getAttribute('genre') == 'Novel') .forEach(print); }
We loop through the descendants and get all elements which contain the attribute
genre
.
$ dart where_fun.dart <book genre='Novel'> <title>Old Goriot</title> <author>Honoré de Balzac</author> <price>9.0</price> </book>
Dart XML findAllElements
The findAllElements
method returns a lazy iterable of the recursive
child elements in document order with the specified tag name.
import 'dart:io'; import 'package:xml/xml.dart'; void main() async { var file = new File('books.xml'); String data = file.readAsStringSync(); var xdoc = XmlDocument.parse(data); var books = xdoc.findAllElements('book'); books .map((e) => e.text.trim().replaceAll(RegExp(r'\s+'), ' ')) .forEach((e) => print("$e ")); print('----------------'); books.map((e) => e.findAllElements('title').single.text).forEach(print); }
In the example, we find all the text data of the book elements and then later all the titles.
$ dart find_all.dart Dune Frank Herbert 8.99 Old Goriot Honoré de Balzac 9.0 ---------------- Dune Old Goriot
Dart build XML document
XML documents can be built with XmlBuilder
.
import 'package:xml/xml.dart'; void main() { var xdoc = buildXml(); print(xdoc.toXmlString(pretty: true, indent: ' ')); } XmlDocument buildXml() { var builder = new XmlBuilder(); builder.processing('xml', 'version="1.0" encoding="utf-8"'); builder.element('bookstore', nest: () { builder.element('book', nest: () { builder.attribute('genre', 'Science Fiction'); builder.element('title', nest: 'Dune'); builder.element('author', nest: 'Frank Herbert'); builder.element('price', nest: 8.99); }); builder.element('book', nest: () { builder.attribute('genre', 'Novel'); builder.element('title', nest: 'Old Goriot'); builder.element('author', nest: 'Honoré de Balzac'); builder.element('price', nest: 9.0); }); }); return builder.buildDocument(); }
The example creates a bookstore XML document.
var builder = new XmlBuilder();
We create an instance of the XmlBuilder
.
builder.element('bookstore', nest: () { builder.element('book', nest: () { builder.attribute('genre', 'Science Fiction'); builder.element('title', nest: 'Dune'); builder.element('author', nest: 'Frank Herbert'); builder.element('price', nest: 8.99); });
A new element is created with element
. Nested elements are created
via the nest
attribute.
return builder.buildDocument();
The document is finalized with buildDocument
.
$ dart builder.dart <?xml version="1.0" encoding="utf-8"?> <bookstore> <book genre="Science Fiction"> <title>Dune</title> <author>Frank Herbert</author> <price>8.99</price> </book> <book genre="Novel"> <title>Old Goriot</title> <author>Honoré de Balzac</author> <price>9.0</price> </book> </bookstore>
Source
Dart xml library documentation
In this article we have worked with XML data in Dart.
Author
List all Dart tutorials.