p-hatan/ mirror

pub repository

mirror.p-hatan.ir

xml

2.0.0

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

Download archive

Readme

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:

  • attributes returns an iterable over the attributes of the current node.
  • children returns 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 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 tag you could write:

var titles = document.findAllElements('title');

This returns a lazy iterator that recursively walks through the XML document and yields all the element nodes with the requested tag name. To extract the textual contents call text:

titles
    .map((node) => node.text)
    .forEach(print);

This prints Growing a Language and Learning XML.

Similary, to compute the total price of all the books one could write the following expression:

var total = document.findAllElements('book')
    .map((node) => double.parse(node.findElements('price').single.text))
    .reduce((a, b) => a + b);
print(total);

Note that this first find all the books, and then extracts the price to avoid counting the price tag that is included in the bookshelf.

Finally, there is the all iterator:

  • iterable returns an iterable over the complete sub-tree of the current node in document order. This includes the attributes of the current node, its children, the children of its children, and so on.

This could for example be used to extract all textual contents from the XML tree:

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

Misc

Supports

  • Standard well-formed XML and HTML.
  • Decodes and encodes commonly used character entities.
  • Querying and traversing API using Dart iterators.

Limitations

  • Doesn't resolve and validate namespace declarations and usage.
  • Doesn't validate schema declarations.
  • Doesn't parse and enforce DTD.

History

This library started as an example of the PetitParser library. To my own surprise various people started to use it to read XML files. In April 2014 I was asked to replace the original dart-xml library from John Evans.

License

The MIT License, see LICENSE.

Changelog

Installing

export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add xml
pubspec.yaml
name: xml
version: 2.0.0
author: Lukas Renggli <renggli@gmail.com>
description: A lightweight library for parsing, traversing, and querying XML documents.
homepage: http://github.com/renggli/dart-xml
environment:
  sdk: '>=1.0.0 <2.0.0'
documentation: http://jenkins.lukas-renggli.ch/job/dart-xml/javadoc
dependencies:
  petitparser: '>=1.0.0 <2.0.0'
dev_dependencies:
  browser: '>=0.10.0 <0.11.0'
  unittest: '>=0.10.0 <0.11.0'
  junitconfiguration: '>=1.0.0 <2.0.0'

Version history

VersionPublishedArchiveSHA-256
0.0.12012-09-25T01:24:58.330770ZDownload7b26c71a383b87d4ebf08294e23e1713b8fd0ed366fd0362d19f564a98b48009
0.1.02012-10-29T19:51:55.780960ZDownloade228f5c2c7af7cc065242ec4337c2ab17fbced57d904e912cb5271d10f344bbe
0.1.32012-11-30T01:25:57.297180ZDownload29d18c7f72c44611eb7daa03d3ef80028899ce08ca8886818055193c17f1061c
0.1.52012-12-10T23:00:58.796990ZDownload575f96f80ef650cb8d552e2695e0c8028726cf01d9112da96994120386715072
0.1.62012-12-17T20:47:22.456410ZDownload30dc633c7415fc271dd9d2ffd935dcfdaa63ce3486f7bf25d9194c50539d3fdd
0.9.02013-02-18T21:12:37.621220ZDownload7ff3db2d2dd9e9242ea62207d0e6b5a777b4b2b44244acec810a348b1767878d
0.9.22013-03-07T20:25:24.844700ZDownload87b5351de1f60018a5d23632eaf919165a5e083bd55245545a6eadc8a46861e7
0.9.32013-03-20T21:13:34.367480ZDownload2b2ad085f4a61c66ee8803aa74303bd362bce9390d235b1b79ea33ed101a5a22
0.9.3+12013-04-27T19:35:42.290130ZDownload86c5a92170691886b8e5edd75812abb42f3c6906d124ba72a40f0180bba865be
0.9.3+22013-05-07T20:24:33.481260ZDownloadba2264540756d8fb761c8e113968104fe817cbc265381f02c5bb7c086cab59e1
0.9.3+32013-08-30T22:23:44.997900ZDownload705e078d53609076d24e234eee10f7179d55bd636b67e26c156c44a6772910e8
0.9.3+42013-08-31T16:24:53.353270ZDownloadbf9d6c2de30832a39a909dc5fa65b521a4fb34bd0062207f5210ecda5245b0fb
0.9.3+52013-08-31T17:19:01.368040ZDownload0b3b072ecaf3431326f6de2ab83bad20b454b8b3a854740d7937e56cbfd0c252
0.9.3+62013-09-10T20:22:24.286110ZDownloadbd4331158827996c36f37523d5404cd55bfb1fa79d3529ae16b1539b03986851
0.9.42013-09-28T17:40:21.278480ZDownload37502b0f766ea750f95d7d46e749a44fed26a616a6b6ed90113e22a4e9da5dd2
0.9.52013-10-26T20:59:39.984890ZDownload6face9cb0dfda6df11faf29368ee1d5cab8ddd6e6ac0511fc2a6cece74296e72
1.0.02014-03-03T02:08:50.577240ZDownloadd70c11b248591a6fd4bd619f7a65ebb9d48322b25f46caeee9386812e398bdf6
2.0.02014-04-21T13:39:50.574320ZDownload4d876b185a041bf73312aa847d50eaf6ef0e73a6b27c583cfbf302805e12a763
2.1.02014-05-18T14:19:59.070930ZDownload65f3b8b8e8a85c6e81cc99ef50947246d52448f1c2e894ea3b7009c4ef8d126d
2.1.12014-05-22T16:53:51.676430ZDownload9a457ad2584bb781ad6105da85eb174eb41c415053c435c50e02d3adcf843ddb
2.1.32014-07-06T08:36:08.215320ZDownloade2c8a0ec819b1300f8eb6d968bfdf6791d5c1006063e501d83ba02abc11da4d4
2.1.42014-08-16T10:38:49.175370ZDownloadbbf37b8538e4ef0331dc209bd953b683b42e767846719e31ff5e26bfce15d05f
2.1.52014-09-06T21:03:10.664670ZDownload25fd7f6b2502a5ac3d578ac676e148e9595a8f6c95d83d067d24351433e5d5f2
2.1.62014-11-14T19:27:39.558730ZDownload8092f729a74e0b780b7642ceaa9d42b9f92c29951205b14526de1d6bd9fe2dd7
2.1.72014-11-15T14:05:57.259510ZDownload12058d13b1ee3d2b2ef3adc69d7070b2e4a091204416c421750c6b80d5335adb
2.2.02014-11-29T08:29:11.267750ZDownload88f4cec54f992cc3025140f0a91baddec1aa688207cc22e443f7c75906ceb18d
2.2.12014-12-14T10:55:24.935730ZDownload82ca7e28158b991a1ab9bd35b59226d92ecc806b04bfb21d2fcd9abd19a4b59f
2.2.22015-01-23T20:54:07.639310ZDownloadd159e6d7cc856596f6ec18d747beff9aefd570e806f64dff1a1fef70fd398222
2.2.32015-02-02T23:10:34.560720ZDownload3d05764a76767cb039c8400a09db0aa6cf981da127f8a38c075f40cc677ac184
2.3.02015-05-15T15:54:31.194ZDownloadb9509111a43f071037b0bbf7d1efd9bb0a1341bfbf27661b3b5da75834c611d1
2.3.12015-05-15T16:07:10.900ZDownload372cd7044a5387e5651e1140c4ceee243dbdf613348d6771ca54a67c4e0209cc
2.3.22015-05-15T17:38:33.135ZDownloaddde4c15db95114f59444cf0c4e1e94a1c12e111f6f97177f0417c77e85a4f700
2.4.02015-10-06T18:18:29.207ZDownloadfe3d69548a898a2de31327ff6c3bbb6148b2bdc2b301473b6aeef1bbfd1c1ea8
2.4.12016-01-17T10:10:10.246ZDownload5eee59bbe5b9fb4c375a803239f062633eba0bcbf5d747bba5b067f37c2c29e0
2.4.22016-03-07T15:50:26.122ZDownloadae7c382c270dc8986d0df5433f3c433f63109e817d42523a820d9576b380a3c2
2.4.32016-04-05T09:23:39.861ZDownload21021658c125784f4c9d1ad7379aa73020808875ca409153ff618a41960d4f38
2.4.42016-11-24T10:32:35.380ZDownload91da53df032406dccf88c691c2fff944638be4b60d5884d666f8497acb5e6b5e
2.5.02016-12-19T17:09:50.949ZDownload2032b1e16e93a4370e40334475ada1550f6070939e1855716cdf848130e9bc83
2.6.02017-08-04T18:36:20.536009ZDownload19049ef7e4c329e6be5b32d69886f35e9b7c74050c368e9b9210d40c72b85a01
3.0.0-alpha2017-12-19T19:29:12.235405ZDownloadcfaec34af17c2b72d937781fd358963c01349893237b01114b6ebec94a4a6ffd
3.0.02018-03-10T11:08:24.098405ZDownload27b1b3905d9de4026b0abc11d6b8f1d342cd319a855899b14dd58b06d091b073
3.0.12018-07-21T14:16:22.882374ZDownload382c0094263113d6e124d80a597e0aa62d8b86ac398b558499bf116e31cf0cfc
3.2.02018-08-01T19:00:28.058334ZDownload3f7fac285ff44c8222a403d72214e8b93bd3f5ded4d23c2765e17aaac32d127e
3.2.12018-09-15T16:31:32.366801ZDownload2e8dd8d05be1779f4b821aeddd399b4ed615a2e172055921f91b017ed1d19e36
3.2.22018-09-27T10:43:32.190543ZDownloadc82b2784de7f2bda08442db6c160fe57b0bb0068d11e73f459a04faa748f30f7
3.2.32018-09-27T11:16:04.113973ZDownload65ced2d31b3b014ccce3e86b66a48f67524d819029663ce333bb4dca47500234
3.2.42019-01-07T21:41:18.603156ZDownload5b14486552a1f2bcbc4d9d8236425e8454ddec0d6df3a355b868f9e324373de7
3.2.52019-01-08T17:19:09.720548ZDownload875ea607302f08409350ca41f2629c108b15a9754fd1188a0b46bc88b8d55421
3.3.02019-01-24T15:38:10.339555ZDownloade2b0670f340410ad55da514b3d1cd5f08f2fa9529bd1cd9d106074e25dba8eed
3.3.12019-01-28T16:52:46.941578ZDownloadc48d9d7a430a64ceb87c09c298e0c7010f69c511648e53c1dab3de6230e0566d
3.4.02019-02-27T22:27:11.249561ZDownload6a97d6ea454dd48ce669477b3ac00fa98e2ab6f77c57689e6e2223696730dfbd
3.4.12019-03-07T06:21:39.957976ZDownload61f0538f19867cff9aebf885bd74fe7eaf531a69885c64b0a87b4148fb6782f3
3.5.02019-05-10T13:37:16.405676ZDownload3a42822cbfa529f8294e6aa1bb941a264161afa469f5dfa54c6f2ea8f2703f4d
3.6.02019-11-30T23:14:53.027563ZDownloadd9e33a640ada4197a9ddc71da51fbd307e37cf15f9d25ed5ef9970aaaaa6ad93
3.6.12019-12-10T06:52:06.429234ZDownload8501c1516b03e10ea0adb38531188e0d2817a58f734b1c1f579f4db5465e1b6b
3.7.02020-01-01T10:06:05.079336ZDownloada11fbb305f75dd200c96d3fd13f9fdb4bc8d8dcdb5331f6ba15c57acb950ef09
4.1.02020-03-30T17:36:13.184636ZDownload77ac18e95b9b58fff42b5feec8fa1cc21f9c46f1394f244de5452ff314696a4f
4.2.02020-05-29T17:29:56.218350ZDownload2b3cb58e5a6b93b99b8d3591539a3ed8192f0e52c050ba92b6e472c916ce3d85
4.3.02020-07-18T10:18:03.398262ZDownload92dffba70672b1a69f5dabb9b81b7a6e507737102cd1e365b8d1b1089f6f34b7
4.4.02020-08-16T19:43:00.741845ZDownloadba9c1a18a909a7f283408d2a80f8fb63f809c8a9cc164b599203b396dec6c7dc
4.4.12020-08-18T20:28:30.733315ZDownloadff2f3b882851cc23e4c32b7cc3193b33fbe988aa0a2b7d2fc9ede6f545894170
4.5.02020-09-04T09:58:33.884988ZDownload1238316e1f90eacb56208e1c788bbb24d2801ca7773a9b6d7ed38ea9a1de43b4
4.5.12020-09-17T21:39:15.234755ZDownload4c4262c7dcf7b66fa8743cdfd37ccac03d6d50d3de4084b1a528de3107540752
5.0.0-nullsafety.12020-11-21T08:05:05.403331ZDownload8a7cc6f9ee52310f9b2759ae7818860108b66c78d2c501561a1cdc3a5de3e961
5.0.02021-02-17T22:22:20.369535ZDownload583426a617c5423a6863a23b6e6cc676f0a0174c38f4fd36de9c6895237ab77f
5.0.12021-02-19T11:00:02.768855ZDownload77139ce98484bfaf6a7bb58a7094541c12f0b06ab962ca31f1c156b4e53ba3c1
5.0.22021-02-19T13:37:44.340685ZDownloadd212eabbd5066ceba6b5187a2ae89a86c609fbba160876b493f2f5db684f0190
5.1.02021-04-05T15:53:58.773619ZDownload88d26fad429944d29b7c418177d156d04bbef049f295cf48130eccc84f0b8b4b
5.1.12021-05-20T16:47:06.537252ZDownload9d2cf96b38e3f523fb78f0102d1d4483f817f3c3a6d81f46bf1b7a430ae8d561
5.1.22021-06-05T12:40:05.210038ZDownload30498604ab00fc50286f8aaf7e077c496550b16ed2f325c14152244882314f03
5.2.02021-08-14T08:30:53.456770ZDownload925e1d7923773fef2f90c5c9ad0f496630f63e03f974a0aaa5fb50c60640c570
5.3.02021-09-10T18:46:38.993940ZDownload78bc0a9ed038e65944b95f4c5d3a72a607d63e1a9f1e8281fa0cfe9d2811c279
5.3.12021-10-16T08:30:07.743586ZDownloadbaa23bcba1ba4ce4b22c0c7a1d9c861e7015cb5169512676da0b85138e72840c
5.4.02022-03-18T18:54:08.313962ZDownloadabc002d0490fc4a74bca4356fca2629323a2595964cfd403f6aa4d120ea07502
5.4.12022-03-19T10:37:30.678247ZDownload80d494c09849dc3f899d227a78c30c5b949b985ededf884cb3f3bcd39f4b447a
6.0.02022-03-28T08:50:07.302357ZDownloaddb7540c353cbf172e6532cae806f0ed31b938fc0cace3f01ac5707418b7553b4
6.0.12022-03-28T08:56:08.620876ZDownload6d1e8d8641b1b4675a2f6ec0d0c6b519a472f0a4f0a2aa8a63d08b64383ee87a
6.1.02022-05-25T08:26:26.056977ZDownloadac0e3f4bf00ba2708c33fbabbbe766300e509f8c82dbd4ab6525039813f7e2fb
6.2.02022-10-23T15:28:24.514859ZDownload1a235945f0831980f690291fe7744e440d73acb20fc18b923cea373f391a2837
6.2.12022-11-08T21:25:50.213162ZDownload0be2cc68212206ab22f700a706782f79a303fb7d4b51279ac394bcc5545f273f
6.2.22022-11-10T08:00:56.961262ZDownload979ee37d622dec6365e2efa4d906c37470995871fe9ae080d967e192d88286b5
6.3.02023-04-24T11:46:02.421859ZDownload5bc72e1e45e941d825fd7468b9b4cc3b9327942649aeb6fc5cdbf135f0a86e84
6.4.02023-08-17T19:32:01.088703ZDownloadb477e76184eaf1ea0a11ebf4b3d937631693b8ea143c759146c4cd512438442d
6.4.12023-08-20T09:46:03.292341ZDownload6a8c45d77aa090cff0380283d4d552761a3f3f6f69b4cbd950cbfcefbeb003a2
6.4.22023-08-20T10:03:58.369917ZDownloadaf5e77e9b83f2f4adc5d3f0a4ece1c7f45a2467b695c2540381bac793e34e556
6.5.02023-12-02T17:36:14.361226ZDownloadb015a8ad1c488f66851d762d3090a21c600e479dc75e68328c52774040cf9226
6.6.02025-06-15T15:17:35.334041ZDownload3202a47961c1a0af6097c9f8c1b492d705248ba309e6f7a72410422c05046851
6.6.12025-08-16T12:56:36.310706ZDownload971043b3a0d3da28727e40ed3e0b5d18b742fa5a68665cca88e74b7876d5e025
7.0.02026-04-25T09:10:48.036664ZDownload2d19c24548c25ea11ed02bfe6d9e0dffe8460aac66231f5174ca970f14b5cc62
7.0.12026-04-25T17:34:36.413972ZDownload67f0aff7be013d107995e9b75bf4e7f2c3ef2dfdb2c8e68024bba0a7fd5756a4
7.1.02026-09-27T14:39:32.170674ZDownload0fe8cc98946def31cbcbc8734aa4067031e6f65ea3a78a508060bfed2200bfeb

Dependencies

petitparser "\u003e=1.0.0 \u003c2.0.0"