Format Code Now
/

XML to Java Class Converter

CV-81

Paste an XML document and press Convert to get classes with getters, setters and Jackson annotations. 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

Java

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 a class with getters and setters, or a record. 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 .java file.
  4. Read it with Jackson, for example new ObjectMapper().readValue(json, Root.class). Feed it the JSON form of your XML, not the XML itself.

How the classes are built

Each object becomes a public class with private fields, getters and setters; nested objects become static nested classes of the first class. Required fields use the primitives int, long, double and boolean; a key that is null in some items or missing from some objects uses the boxed type. A whole number beyond long becomes BigInteger, and arrays become List<T>. Java keywords such as class get a trailing underscore, and the record option writes records instead of classes (Java 16 or later).

How the XML becomes classes

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. Comments, processing instructions and the DOCTYPE are dropped, and namespace prefixes stay in the name. The XML is read into JSON first, using the same rules as the XML to JSON converter, and the code is built from that JSON. In Java, @id becomes a field named id with @JsonProperty("@id"), and #text becomes text.

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; switch to java.time types and register the Jackson module for them yourself. 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 Java class?

Paste the XML into the input box, choose your options and press Convert to get the classes. Read it with Jackson, for example new ObjectMapper().readValue(json, Root.class). The classes follow the JSON form of the XML. The conversion happens in your browser.

Should I choose a class or a record in Java?

Choose the record option on Java 16 or later if you want immutable data with the keys as components. The class option writes private fields with getters and setters and works on any Java version. This applies to the JSON form of your XML.

Why does a Java field use Integer instead of int?

A key that is null in some items or missing from some objects uses the boxed type, such as Integer or Boolean, because a primitive cannot hold null. Keys present in every item use the primitives int, long, double and boolean. This applies to the JSON form of your XML.

What happens to a number beyond long in Java?

It becomes BigInteger, with a comment on the field, so no digits are lost on the Java side. Decimals use double. Java keywords used as keys, such as class, get a trailing underscore. This applies to the JSON form of your XML.

Are numbers in XML text typed as numbers?

By default, yes: text that is exactly a number, such as 59.90 or 3, becomes a number, and true or false becomes a boolean. Text with a leading zero or plus sign, like 007, stays a string. Turn the option off to make every value a string. The Java classes are written from that converted data.

What happens to XML attributes in the generated code?

Each attribute becomes a key that starts with @, such as @id, so it is a member of the generated type next to the child elements. The member name drops the @, and the original key is kept in the code where the language allows it. The Java 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 private fields, getters and setters, or a record; nested objects become static nested classes. Field names are camelCase and the original key goes in a Jackson @JsonProperty annotation whenever the name differs, so Jackson annotations must be on the class path. Required fields use int, long, double and boolean, and a key that is null in some items or missing from some objects uses the boxed type. A whole number beyond long becomes BigInteger. Arrays become List<T>, and mixed types and empty arrays become Object. Everything runs in your browser; nothing is uploaded.

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