pub repository
mirror.p-hatan.irxml
2.0.0A lightweight library for parsing, traversing, and querying XML documents.
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:
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 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:
iterablereturns 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 xmlpubspec.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
| Version | Published | Archive | SHA-256 |
|---|---|---|---|
| 0.0.1 | 2012-09-25T01:24:58.330770Z | Download | 7b26c71a383b87d4ebf08294e23e1713b8fd0ed366fd0362d19f564a98b48009 |
| 0.1.0 | 2012-10-29T19:51:55.780960Z | Download | e228f5c2c7af7cc065242ec4337c2ab17fbced57d904e912cb5271d10f344bbe |
| 0.1.3 | 2012-11-30T01:25:57.297180Z | Download | 29d18c7f72c44611eb7daa03d3ef80028899ce08ca8886818055193c17f1061c |
| 0.1.5 | 2012-12-10T23:00:58.796990Z | Download | 575f96f80ef650cb8d552e2695e0c8028726cf01d9112da96994120386715072 |
| 0.1.6 | 2012-12-17T20:47:22.456410Z | Download | 30dc633c7415fc271dd9d2ffd935dcfdaa63ce3486f7bf25d9194c50539d3fdd |
| 0.9.0 | 2013-02-18T21:12:37.621220Z | Download | 7ff3db2d2dd9e9242ea62207d0e6b5a777b4b2b44244acec810a348b1767878d |
| 0.9.2 | 2013-03-07T20:25:24.844700Z | Download | 87b5351de1f60018a5d23632eaf919165a5e083bd55245545a6eadc8a46861e7 |
| 0.9.3 | 2013-03-20T21:13:34.367480Z | Download | 2b2ad085f4a61c66ee8803aa74303bd362bce9390d235b1b79ea33ed101a5a22 |
| 0.9.3+1 | 2013-04-27T19:35:42.290130Z | Download | 86c5a92170691886b8e5edd75812abb42f3c6906d124ba72a40f0180bba865be |
| 0.9.3+2 | 2013-05-07T20:24:33.481260Z | Download | ba2264540756d8fb761c8e113968104fe817cbc265381f02c5bb7c086cab59e1 |
| 0.9.3+3 | 2013-08-30T22:23:44.997900Z | Download | 705e078d53609076d24e234eee10f7179d55bd636b67e26c156c44a6772910e8 |
| 0.9.3+4 | 2013-08-31T16:24:53.353270Z | Download | bf9d6c2de30832a39a909dc5fa65b521a4fb34bd0062207f5210ecda5245b0fb |
| 0.9.3+5 | 2013-08-31T17:19:01.368040Z | Download | 0b3b072ecaf3431326f6de2ab83bad20b454b8b3a854740d7937e56cbfd0c252 |
| 0.9.3+6 | 2013-09-10T20:22:24.286110Z | Download | bd4331158827996c36f37523d5404cd55bfb1fa79d3529ae16b1539b03986851 |
| 0.9.4 | 2013-09-28T17:40:21.278480Z | Download | 37502b0f766ea750f95d7d46e749a44fed26a616a6b6ed90113e22a4e9da5dd2 |
| 0.9.5 | 2013-10-26T20:59:39.984890Z | Download | 6face9cb0dfda6df11faf29368ee1d5cab8ddd6e6ac0511fc2a6cece74296e72 |
| 1.0.0 | 2014-03-03T02:08:50.577240Z | Download | d70c11b248591a6fd4bd619f7a65ebb9d48322b25f46caeee9386812e398bdf6 |
| 2.0.0 | 2014-04-21T13:39:50.574320Z | Download | 4d876b185a041bf73312aa847d50eaf6ef0e73a6b27c583cfbf302805e12a763 |
| 2.1.0 | 2014-05-18T14:19:59.070930Z | Download | 65f3b8b8e8a85c6e81cc99ef50947246d52448f1c2e894ea3b7009c4ef8d126d |
| 2.1.1 | 2014-05-22T16:53:51.676430Z | Download | 9a457ad2584bb781ad6105da85eb174eb41c415053c435c50e02d3adcf843ddb |
| 2.1.3 | 2014-07-06T08:36:08.215320Z | Download | e2c8a0ec819b1300f8eb6d968bfdf6791d5c1006063e501d83ba02abc11da4d4 |
| 2.1.4 | 2014-08-16T10:38:49.175370Z | Download | bbf37b8538e4ef0331dc209bd953b683b42e767846719e31ff5e26bfce15d05f |
| 2.1.5 | 2014-09-06T21:03:10.664670Z | Download | 25fd7f6b2502a5ac3d578ac676e148e9595a8f6c95d83d067d24351433e5d5f2 |
| 2.1.6 | 2014-11-14T19:27:39.558730Z | Download | 8092f729a74e0b780b7642ceaa9d42b9f92c29951205b14526de1d6bd9fe2dd7 |
| 2.1.7 | 2014-11-15T14:05:57.259510Z | Download | 12058d13b1ee3d2b2ef3adc69d7070b2e4a091204416c421750c6b80d5335adb |
| 2.2.0 | 2014-11-29T08:29:11.267750Z | Download | 88f4cec54f992cc3025140f0a91baddec1aa688207cc22e443f7c75906ceb18d |
| 2.2.1 | 2014-12-14T10:55:24.935730Z | Download | 82ca7e28158b991a1ab9bd35b59226d92ecc806b04bfb21d2fcd9abd19a4b59f |
| 2.2.2 | 2015-01-23T20:54:07.639310Z | Download | d159e6d7cc856596f6ec18d747beff9aefd570e806f64dff1a1fef70fd398222 |
| 2.2.3 | 2015-02-02T23:10:34.560720Z | Download | 3d05764a76767cb039c8400a09db0aa6cf981da127f8a38c075f40cc677ac184 |
| 2.3.0 | 2015-05-15T15:54:31.194Z | Download | b9509111a43f071037b0bbf7d1efd9bb0a1341bfbf27661b3b5da75834c611d1 |
| 2.3.1 | 2015-05-15T16:07:10.900Z | Download | 372cd7044a5387e5651e1140c4ceee243dbdf613348d6771ca54a67c4e0209cc |
| 2.3.2 | 2015-05-15T17:38:33.135Z | Download | dde4c15db95114f59444cf0c4e1e94a1c12e111f6f97177f0417c77e85a4f700 |
| 2.4.0 | 2015-10-06T18:18:29.207Z | Download | fe3d69548a898a2de31327ff6c3bbb6148b2bdc2b301473b6aeef1bbfd1c1ea8 |
| 2.4.1 | 2016-01-17T10:10:10.246Z | Download | 5eee59bbe5b9fb4c375a803239f062633eba0bcbf5d747bba5b067f37c2c29e0 |
| 2.4.2 | 2016-03-07T15:50:26.122Z | Download | ae7c382c270dc8986d0df5433f3c433f63109e817d42523a820d9576b380a3c2 |
| 2.4.3 | 2016-04-05T09:23:39.861Z | Download | 21021658c125784f4c9d1ad7379aa73020808875ca409153ff618a41960d4f38 |
| 2.4.4 | 2016-11-24T10:32:35.380Z | Download | 91da53df032406dccf88c691c2fff944638be4b60d5884d666f8497acb5e6b5e |
| 2.5.0 | 2016-12-19T17:09:50.949Z | Download | 2032b1e16e93a4370e40334475ada1550f6070939e1855716cdf848130e9bc83 |
| 2.6.0 | 2017-08-04T18:36:20.536009Z | Download | 19049ef7e4c329e6be5b32d69886f35e9b7c74050c368e9b9210d40c72b85a01 |
| 3.0.0-alpha | 2017-12-19T19:29:12.235405Z | Download | cfaec34af17c2b72d937781fd358963c01349893237b01114b6ebec94a4a6ffd |
| 3.0.0 | 2018-03-10T11:08:24.098405Z | Download | 27b1b3905d9de4026b0abc11d6b8f1d342cd319a855899b14dd58b06d091b073 |
| 3.0.1 | 2018-07-21T14:16:22.882374Z | Download | 382c0094263113d6e124d80a597e0aa62d8b86ac398b558499bf116e31cf0cfc |
| 3.2.0 | 2018-08-01T19:00:28.058334Z | Download | 3f7fac285ff44c8222a403d72214e8b93bd3f5ded4d23c2765e17aaac32d127e |
| 3.2.1 | 2018-09-15T16:31:32.366801Z | Download | 2e8dd8d05be1779f4b821aeddd399b4ed615a2e172055921f91b017ed1d19e36 |
| 3.2.2 | 2018-09-27T10:43:32.190543Z | Download | c82b2784de7f2bda08442db6c160fe57b0bb0068d11e73f459a04faa748f30f7 |
| 3.2.3 | 2018-09-27T11:16:04.113973Z | Download | 65ced2d31b3b014ccce3e86b66a48f67524d819029663ce333bb4dca47500234 |
| 3.2.4 | 2019-01-07T21:41:18.603156Z | Download | 5b14486552a1f2bcbc4d9d8236425e8454ddec0d6df3a355b868f9e324373de7 |
| 3.2.5 | 2019-01-08T17:19:09.720548Z | Download | 875ea607302f08409350ca41f2629c108b15a9754fd1188a0b46bc88b8d55421 |
| 3.3.0 | 2019-01-24T15:38:10.339555Z | Download | e2b0670f340410ad55da514b3d1cd5f08f2fa9529bd1cd9d106074e25dba8eed |
| 3.3.1 | 2019-01-28T16:52:46.941578Z | Download | c48d9d7a430a64ceb87c09c298e0c7010f69c511648e53c1dab3de6230e0566d |
| 3.4.0 | 2019-02-27T22:27:11.249561Z | Download | 6a97d6ea454dd48ce669477b3ac00fa98e2ab6f77c57689e6e2223696730dfbd |
| 3.4.1 | 2019-03-07T06:21:39.957976Z | Download | 61f0538f19867cff9aebf885bd74fe7eaf531a69885c64b0a87b4148fb6782f3 |
| 3.5.0 | 2019-05-10T13:37:16.405676Z | Download | 3a42822cbfa529f8294e6aa1bb941a264161afa469f5dfa54c6f2ea8f2703f4d |
| 3.6.0 | 2019-11-30T23:14:53.027563Z | Download | d9e33a640ada4197a9ddc71da51fbd307e37cf15f9d25ed5ef9970aaaaa6ad93 |
| 3.6.1 | 2019-12-10T06:52:06.429234Z | Download | 8501c1516b03e10ea0adb38531188e0d2817a58f734b1c1f579f4db5465e1b6b |
| 3.7.0 | 2020-01-01T10:06:05.079336Z | Download | a11fbb305f75dd200c96d3fd13f9fdb4bc8d8dcdb5331f6ba15c57acb950ef09 |
| 4.1.0 | 2020-03-30T17:36:13.184636Z | Download | 77ac18e95b9b58fff42b5feec8fa1cc21f9c46f1394f244de5452ff314696a4f |
| 4.2.0 | 2020-05-29T17:29:56.218350Z | Download | 2b3cb58e5a6b93b99b8d3591539a3ed8192f0e52c050ba92b6e472c916ce3d85 |
| 4.3.0 | 2020-07-18T10:18:03.398262Z | Download | 92dffba70672b1a69f5dabb9b81b7a6e507737102cd1e365b8d1b1089f6f34b7 |
| 4.4.0 | 2020-08-16T19:43:00.741845Z | Download | ba9c1a18a909a7f283408d2a80f8fb63f809c8a9cc164b599203b396dec6c7dc |
| 4.4.1 | 2020-08-18T20:28:30.733315Z | Download | ff2f3b882851cc23e4c32b7cc3193b33fbe988aa0a2b7d2fc9ede6f545894170 |
| 4.5.0 | 2020-09-04T09:58:33.884988Z | Download | 1238316e1f90eacb56208e1c788bbb24d2801ca7773a9b6d7ed38ea9a1de43b4 |
| 4.5.1 | 2020-09-17T21:39:15.234755Z | Download | 4c4262c7dcf7b66fa8743cdfd37ccac03d6d50d3de4084b1a528de3107540752 |
| 5.0.0-nullsafety.1 | 2020-11-21T08:05:05.403331Z | Download | 8a7cc6f9ee52310f9b2759ae7818860108b66c78d2c501561a1cdc3a5de3e961 |
| 5.0.0 | 2021-02-17T22:22:20.369535Z | Download | 583426a617c5423a6863a23b6e6cc676f0a0174c38f4fd36de9c6895237ab77f |
| 5.0.1 | 2021-02-19T11:00:02.768855Z | Download | 77139ce98484bfaf6a7bb58a7094541c12f0b06ab962ca31f1c156b4e53ba3c1 |
| 5.0.2 | 2021-02-19T13:37:44.340685Z | Download | d212eabbd5066ceba6b5187a2ae89a86c609fbba160876b493f2f5db684f0190 |
| 5.1.0 | 2021-04-05T15:53:58.773619Z | Download | 88d26fad429944d29b7c418177d156d04bbef049f295cf48130eccc84f0b8b4b |
| 5.1.1 | 2021-05-20T16:47:06.537252Z | Download | 9d2cf96b38e3f523fb78f0102d1d4483f817f3c3a6d81f46bf1b7a430ae8d561 |
| 5.1.2 | 2021-06-05T12:40:05.210038Z | Download | 30498604ab00fc50286f8aaf7e077c496550b16ed2f325c14152244882314f03 |
| 5.2.0 | 2021-08-14T08:30:53.456770Z | Download | 925e1d7923773fef2f90c5c9ad0f496630f63e03f974a0aaa5fb50c60640c570 |
| 5.3.0 | 2021-09-10T18:46:38.993940Z | Download | 78bc0a9ed038e65944b95f4c5d3a72a607d63e1a9f1e8281fa0cfe9d2811c279 |
| 5.3.1 | 2021-10-16T08:30:07.743586Z | Download | baa23bcba1ba4ce4b22c0c7a1d9c861e7015cb5169512676da0b85138e72840c |
| 5.4.0 | 2022-03-18T18:54:08.313962Z | Download | abc002d0490fc4a74bca4356fca2629323a2595964cfd403f6aa4d120ea07502 |
| 5.4.1 | 2022-03-19T10:37:30.678247Z | Download | 80d494c09849dc3f899d227a78c30c5b949b985ededf884cb3f3bcd39f4b447a |
| 6.0.0 | 2022-03-28T08:50:07.302357Z | Download | db7540c353cbf172e6532cae806f0ed31b938fc0cace3f01ac5707418b7553b4 |
| 6.0.1 | 2022-03-28T08:56:08.620876Z | Download | 6d1e8d8641b1b4675a2f6ec0d0c6b519a472f0a4f0a2aa8a63d08b64383ee87a |
| 6.1.0 | 2022-05-25T08:26:26.056977Z | Download | ac0e3f4bf00ba2708c33fbabbbe766300e509f8c82dbd4ab6525039813f7e2fb |
| 6.2.0 | 2022-10-23T15:28:24.514859Z | Download | 1a235945f0831980f690291fe7744e440d73acb20fc18b923cea373f391a2837 |
| 6.2.1 | 2022-11-08T21:25:50.213162Z | Download | 0be2cc68212206ab22f700a706782f79a303fb7d4b51279ac394bcc5545f273f |
| 6.2.2 | 2022-11-10T08:00:56.961262Z | Download | 979ee37d622dec6365e2efa4d906c37470995871fe9ae080d967e192d88286b5 |
| 6.3.0 | 2023-04-24T11:46:02.421859Z | Download | 5bc72e1e45e941d825fd7468b9b4cc3b9327942649aeb6fc5cdbf135f0a86e84 |
| 6.4.0 | 2023-08-17T19:32:01.088703Z | Download | b477e76184eaf1ea0a11ebf4b3d937631693b8ea143c759146c4cd512438442d |
| 6.4.1 | 2023-08-20T09:46:03.292341Z | Download | 6a8c45d77aa090cff0380283d4d552761a3f3f6f69b4cbd950cbfcefbeb003a2 |
| 6.4.2 | 2023-08-20T10:03:58.369917Z | Download | af5e77e9b83f2f4adc5d3f0a4ece1c7f45a2467b695c2540381bac793e34e556 |
| 6.5.0 | 2023-12-02T17:36:14.361226Z | Download | b015a8ad1c488f66851d762d3090a21c600e479dc75e68328c52774040cf9226 |
| 6.6.0 | 2025-06-15T15:17:35.334041Z | Download | 3202a47961c1a0af6097c9f8c1b492d705248ba309e6f7a72410422c05046851 |
| 6.6.1 | 2025-08-16T12:56:36.310706Z | Download | 971043b3a0d3da28727e40ed3e0b5d18b742fa5a68665cca88e74b7876d5e025 |
| 7.0.0 | 2026-04-25T09:10:48.036664Z | Download | 2d19c24548c25ea11ed02bfe6d9e0dffe8460aac66231f5174ca970f14b5cc62 |
| 7.0.1 | 2026-04-25T17:34:36.413972Z | Download | 67f0aff7be013d107995e9b75bf4e7f2c3ef2dfdb2c8e68024bba0a7fd5756a4 |
| 7.1.0 | 2026-09-27T14:39:32.170674Z | Download | 0fe8cc98946def31cbcbc8734aa4067031e6f65ea3a78a508060bfed2200bfeb |
Dependencies
petitparser "\u003e=1.0.0 \u003c2.0.0"