XML to Dart
Generate null-safe Dart (Flutter) model classes from an XML sample.
XML to Dart – Flutter models for XML APIs
The XML to Dart converter generates null-safe Dart classes for XML data. Each element type becomes a class with typed fields, a named constructor, a fromJson(Map<String, dynamic>) factory and a toJson() method, so XML feeds can be handled with the same model pattern you use for JSON in Flutter.
How XML maps to types
XML has no native types, so the converter reads every element and attribute value and infers one: whole numbers become integers, decimals become floating-point numbers, true/false become booleans, and everything else stays a string. Attributes are kept apart from child elements (they appear as @name keys internally), an element that has both attributes and text gets a dedicated text field, and repeated sibling elements such as several item nodes become a list. Namespace prefixes are stripped from names so ns:customer and customer produce the same type.
Reading XML in Dart
The xml package on pub.dev parses documents into a DOM; a short helper (or a package such as xml2json) can turn that DOM into a Map with attributes stored as @name and mixed text as #text. Pass that map to Order.fromJson(map) and you get typed objects. Number fields are read as num and converted with toInt() or toDouble(); if your helper leaves all values as strings, change those lines to int.parse and double.parse.
Options
- final fields + const constructor for immutable models (recommended for Flutter widgets and state).
- DateTime for timestamps parses ISO 8601 values with
DateTime.parse. - copyWith() adds a method that returns a modified copy — handy with Riverpod, Bloc or Provider.
Use cases
Building a Flutter podcast app on RSS feeds, reading weather or transit data published as XML, consuming a SOAP backend from mobile, or importing GPX and KML-style data.
Tips
Repeated elements become List<T> only when the sample contains at least two siblings, so include several items. Keys such as @id and #text are kept in the json['...'] lookups and escaped where needed, while field names are clean camelCase.
Related tools
Try JSON to Dart for JSON APIs or convert the document with XML to JSON.
Frequently Asked Questions
Is my XML uploaded anywhere?
No. The XML is parsed and converted by JavaScript running in your browser tab. Nothing is sent to a server, so internal feeds, SOAP payloads and configuration files stay on your machine.
Why does the generated code use fromJson for XML?
Dart has no built-in XML data binding, so the common approach is to convert the parsed XML into a Map first. The fromJson/toJson naming matches what Flutter developers already use and works with any Map.
Which Dart version is required?
Dart 2.12 or newer for sound null safety; everything works in Dart 3 and current Flutter releases.
How are attributes and text named?
An attribute such as id becomes a field id read from json['@id']; the text of an element with attributes becomes a field named text read from json['#text'].