XML to Dart Converter - Generate Dart Classes from XML Online
Convert XML data to Dart classes with serialization support from XML documents. Free online tool.
XML to Dart converter, Dart classes, Flutter XML, Dart serialization, XML parsing
How to Convert XML to Dart Classes - Step by Step Guide
Input Your XML Schema
Start by adding your XML data that needs to be converted to Dart classes. The tool analyzes the structure to generate appropriate classes for Flutter development.
Example: Try This XML Data
Copy and paste this XML example to see how it works:
<?xml version="1.0" encoding="UTF-8"?> <app> <user id="123"> <name>Flutter Developer</name> <email>[email protected]</email> <age>25</age> <is_premium>true</is_premium> <preferences> <theme>dark</theme> <language>dart</language> <notifications>false</notifications> </preferences> </user> </app>
Check the Types
There are no settings to fiddle with. The converter looks at every copy of each element and picks a type for each field of the Dart classes:
Get Generated Dart Code
For the Step 1 example you get a Dart class for each element that has attributes or children: Root, User and Preferences.
Dart Code Output
The User class from the generated file (Root and Preferences look the same):
class User {
final int? id; // attribute id
final String? name; // element <name>
final String? email; // element <email>
final int? age; // element <age>
final bool? isPremium; // element <is_premium>
final Preferences? preferences; // element <preferences>
const User({
this.id,
this.name,
this.email,
this.age,
this.isPremium,
this.preferences,
});
factory User.fromXml(XmlElement element) {
return User(
id: _toInt(element.getAttribute('id')),
name: _first(element, 'name', _ownText),
email: _first(element, 'email', _ownText),
age: _toInt(_first(element, 'age', _ownText)),
isPremium: _toBool(_first(element, 'is_premium', _ownText)),
preferences: _first(element, 'preferences', Preferences.fromXml),
);
}
void toXml(XmlBuilder builder, String elementName) {
builder.element(elementName, nest: () {
if (id != null) builder.attribute('id', '$id');
if (name != null) builder.element('name', nest: '$name');
if (email != null) builder.element('email', nest: '$email');
if (age != null) builder.element('age', nest: '$age');
if (isPremium != null) builder.element('is_premium', nest: '$isPremium');
preferences?.toXml(builder, 'preferences');
});
}
}Copy or Download Dart Code
Perfect! Now you can use your Dart classes in your Flutter applications for XML processing.
What is XML to Dart Conversion?
XML to Dart conversion turns an XML document into Dart classes with typed, null-safe fields. The generated code reads the XML with package:xml into those classes and can write them back out, so you work with objects instead of walking the element tree by hand.
The generated Dart classes include proper constructors, nullable types, and nested class definitions, making it easy to work with XML data in a Dart-native way while maintaining type safety and Flutter compatibility.
Frequently Asked Questions
What exactly does the generated Dart file contain?
One class per XML element that has attributes or child elements, with final, nullable fields and a const constructor. Each class gets a fromXml factory that reads an XmlElement from package:xml and a toXml method that writes it back through an XmlBuilder. Elements that only hold text become plain fields, repeated elements become lists, and a handful of small helper functions sit at the top of the file.
How are the Dart types picked?
Every copy of an element is looked at, not just the first one. A field is int only if every value is a plain whole number that fits in 64 bits, double if they are all decimal numbers, and bool only for the exact words true and false. Anything else, including values like 00501 or 0x1F, numbers too long for 64 bits, and elements that are sometimes empty, stays a String so no digits or leading zeros get lost.
Will a long ID like 8901240544102066246 survive?
Yes on the Dart VM and in Flutter mobile or desktop apps, where int is 64-bit, so the field is typed int and keeps every digit. Flutter web compiles int to a JavaScript number, which only holds about 16 digits, so the file adds a short note when it sees such a value. If you target the web, change that field to String.
Which Dart and package versions does the code need?
Dart 3 or newer and the xml package from pub.dev (run dart pub add xml). The output was checked against xml 6.x and 7.x, and it does not depend on code generation or build_runner.
What happens to names that are not valid in Dart?
Element and attribute names are turned into lowerCamelCase fields and PascalCase classes. Dart keywords such as class or default get a Value suffix, names starting with a digit get a prefix, accents are dropped because Dart identifiers are ASCII only, and clashes get a number. The original XML name is still used when reading and writing, so nothing changes in the document itself.
Is my XML uploaded anywhere?
No. The conversion runs in your browser as you type, and the Dart code is generated on your machine. Very large documents depend on your browser memory, so a file of tens of megabytes may feel slow.