What XML to Dart does
XML to Dart turns a sample XML document into Dart code: one class with a named constructor per object, with a type for every field inferred from the value in the sample.
XML to Dart examples
The built-in sample
Input
<users><user id="1"><name>Ada</name></user><user id="2"><name>Lin</name></user></users>Output
class Root {
final String _id;
final String name;
Root({required this._id, required this.name});
}This is the input that the Load sample button fills in.
Attributes, repeated elements and empty elements
Input
<order id="7" paid="false"><total>19.5</total><item sku="A1"><qty>2</qty></item><item sku="B2"><qty>1</qty><gift/></item><note/></order>Output
class Item {
final String _sku;
final String qty;
final String gift;
Item({required this._sku, required this.qty, required this.gift});
}
class Root {
final String _id;
final String _paid;
final String total;
final List<Item> item;
final String note;
Root({required this._id, required this._paid, required this.total, required this.item, required this.note});
}In the sample, id, paid and sku are attributes, item repeats, and gift and note are empty elements. Every value is typed as a string, because XML does not carry types.
Each output was produced by running this tool on the input above it.
How it works
The sample is walked recursively and each value is mapped to a Dart type:
| Sample value | Dart type |
|---|---|
| String | String |
| Whole number | int |
| Decimal number | double |
| true / false | bool |
| null, or an empty array’s items | dynamic |
| Array of T | List<T> |
| Object | a new named type |
XML has no data types, so every leaf value is generated as the Dart string type. Attributes become fields too: the @ prefix becomes an underscore (_id). Repeated elements become lists, and the root element itself is always called Root.
Reading XML into the generated types
There is no XML binding in the Dart SDK. With the xml package you read attributes and child elements yourself and pass them to the constructor.
Details and limits
- A name that starts with an underscore is private to its library in Dart, and most SDK versions reject a named parameter such as
required this._id. Rename such fields. - A key that is a reserved word, such as
class, is emitted unchanged and must be renamed. - Null values and empty arrays are typed
dynamic. Replace it with a nullable type such asString?. - A type is named after the key that holds the object:
itemsgives a type calledItems, with no attempt to make it singular. The top-level type is alwaysRoot. - If two different objects sit under the same key name in different places, two types with the same name are generated. Rename one of them.
- Objects in an array are merged into one type. A field that only some of them have is still generated as a required field.
- Input that is not valid XML stops the tool, and the status line shows where the problem is. The XML Validator page lists the usual causes.
Questions
Is my data uploaded when I use XML to Dart?
No. The tool runs in your browser, so what you paste or open is processed on your own device and is not sent to a server.
What does XML to Dart do?
XML to Dart turns a sample XML document into Dart code: one class with a named constructor per object, with a type for every field inferred from the value in the sample.
Are there any limits I should know about?
A name that starts with an underscore is private to its library in Dart, and most SDK versions reject a named parameter such as required this._id. Rename such fields. A key that is a reserved word, such as class, is emitted unchanged and must be renamed.
Is there a size limit?
There is no fixed size limit. Everything happens on your device, so very large inputs depend on its memory and can take longer on a phone than on a computer.
Does it work on phones, and is it free?
Yes. It works in any current browser on Android phones, iPhones, tablets and computers. It is free and needs no account or sign-up.