Format Code Now
/
ConvertXML to Dart Next: XML to Go ›

XML to Dart Class Generator

CV-79

Paste an XML document and press Convert to get classes with fromJson and toJson. The XML is read into JSON first, so attributes, text and repeated elements follow the rules below, and it runs in your browser.

Runs in your browser. Nothing is uploaded.

XML

Dart

How to use it

  1. Paste an XML document, or open an .xml file. Use one that shows every element you expect, with repeating elements repeated.
  2. Choose fromJson and toJson written out, or json_serializable annotations, and keep or drop the final fields. Leave typed values on to read 59.90 as a number and true as a boolean, or turn it off to keep every value as text.
  3. Press Convert, then copy the result or download it as a .dart file.
  4. Decode with jsonDecode and call Root.fromJson(map), then call toJson() to write it back. Feed it the JSON form of your XML, not the XML itself.

How the classes are built

Each object becomes a class with fields in camelCase, a constructor with named parameters, a fromJson factory and a toJson method. A key that is null or missing in some items gets a nullable type (T?), and a key missing from some objects is also left out of toJson while it is null. Mixed types, empty arrays and keys that are always null become dynamic, and Dart words such as class or default get a trailing underscore. The json_serializable choice writes @JsonSerializable and @JsonKey annotations and a part directive; you run build_runner to create the .g.dart file.

How the XML becomes classes

The root element becomes the single top-level key, so the first type holds one member named after it. An attribute becomes a key that starts with @, and text that sits next to attributes or child elements becomes #text. An element that repeats under the same parent becomes a list, while an element that appears once stays a single value. Text that is exactly a number or true/false is typed as such when the option is on (the default); a leading zero or plus sign keeps it as a string. In Dart, @id becomes a field named id, and fromJson reads json['@id'].

What a sample cannot tell it

The result only reflects the sample you paste: a key that never appears has no member, and a number that is always whole in the sample is typed as whole even if other data holds decimals. Dates stay String; parse them with DateTime.parse in the fromJson code if you need DateTime values. An element that appears once in your sample is written as a single value; if real documents can repeat it, paste a sample where it appears at least twice so it becomes a list.

Questions people ask

How do I convert XML to a Dart class?

Paste the XML into the input box, choose your options and press Convert to get the classes. Decode with jsonDecode and call Root.fromJson(map), then call toJson() to write it back. The classes follow the JSON form of the XML. The conversion happens in your browser.

How are numbers typed in the Dart classes?

Whole numbers become int, and numbers with a decimal point become double. fromJson reads doubles through num, so a JSON 20 still loads into a double field. An integer beyond int64 becomes double with a comment on the field. This applies to the JSON form of your XML.

Why is a Dart field typed dynamic?

Mixed types, an empty array and a key that is always null have no single type in the sample, so the field is dynamic. Replace it with the type you expect, or paste a sample where the key holds a value. This applies to the JSON form of your XML.

Are the fields final in the Dart output?

By default yes, with a const constructor, which suits immutable models. Turn the final option off to get plain mutable fields and a normal constructor. This applies to the JSON form of your XML.

Why is an element a list in one XML sample and a single value in another?

XML does not say how many times an element may repeat. An element becomes a list only when it appears more than once under the same parent in your sample. To get a list type, paste a sample where the element appears at least twice. The Dart classes are written from that converted data.

Does it read the XML declaration, comments or namespaces?

The declaration, comments, processing instructions and DOCTYPE are read and then dropped. Namespace prefixes stay as part of the element name, and namespaces are not resolved. Custom entities are an error; only the five built-in ones and numeric character references are decoded. The Dart classes are written from that converted data.

About this tool

The XML is read first and turned into the same JSON that the XML to JSON converter writes, and the code is generated from that. The root element becomes the single top-level key, so the root class holds one field named after it. An attribute becomes a key that starts with @, text next to attributes or child elements becomes #text, an element that appears more than once under one parent becomes a list, and one that appears once stays a single value, so a sample with one item gives a single object where another sample would give a list. Comments, processing instructions and the DOCTYPE are dropped. With the typed-values option on (the default), text that is exactly a number or true/false becomes a number or boolean; text with a leading zero or plus sign stays a string, and turning the option off makes every value a string. The result describes the converted JSON, not an XmlSerializer or JAXB mapping. Every object becomes a class with fields in camelCase, a constructor with named parameters, a fromJson factory and a toJson method, working on Map<String, dynamic>. Types are int, double, String, bool and List<T>; a number written with a decimal point, or beyond int64, becomes double (read through num, so 20 still loads into a double field), and a comment marks integers beyond int64. A key that is null or missing in some items gets a nullable type (T?) and is left out of toJson when it is missing; mixed types, always-null keys and empty arrays become dynamic. Dart words such as class or default get a trailing underscore. The json_serializable choice writes the annotations and a part directive instead, and you still run build_runner to create the .g.dart file. It does not detect dates. Everything runs in your browser; nothing is uploaded.

Format Code Now: free, no ads, no accounts. Your input stays in your browser.