XML to Java

Generate Java types from XML automatically.

Local Your input is processed locally in your browser. Found a problem? Report an issue

What XML to Java does

XML to Java turns a sample XML document into Java code: one class with public fields per object, with a type for every field inferred from the value in the sample.

XML to Java examples

The built-in sample

Input

<users><user id="1"><name>Ada</name></user><user id="2"><name>Lin</name></user></users>

Output

import java.util.List;

public class Root {
    public String _id;
    public String 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

import java.util.List;

public class Item {
    public String _sku;
    public String qty;
    public String gift;
}

public class Root {
    public String _id;
    public String _paid;
    public String total;
    public List<Item> item;
    public String 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 Java type:

Sample valueJava type
StringString
Whole numberint
Decimal numberdouble
true / falseboolean
null, or an empty array’s itemsObject
Array of TList<T>
Objecta new named type

XML has no data types, so every leaf value is generated as the Java 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

To bind XML directly with JAXB, add @XmlRootElement to the root class, @XmlAttribute to the fields that came from attributes (the ones starting with an underscore) and @XmlElement to the others.

Details and limits

  • A Java file may contain only one public top-level class. Put each generated class in its own file, or remove public from all but one.
  • A key that is not a valid identifier is renamed with underscores. Map it back with @JsonProperty("order-id") (Jackson) or @SerializedName (Gson).
  • A key that is a reserved word, such as class, is emitted unchanged and will not compile until you rename it.
  • Whole numbers are int. Change the field to long for values above about two billion.
  • A type is named after the key that holds the object: items gives a type called Items, with no attempt to make it singular. The top-level type is always Root.
  • 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 Java?

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 Java do?

XML to Java turns a sample XML document into Java code: one class with public fields per object, with a type for every field inferred from the value in the sample.

Are there any limits I should know about?

A Java file may contain only one public top-level class. Put each generated class in its own file, or remove public from all but one. A key that is not a valid identifier is renamed with underscores. Map it back with @JsonProperty("order-id") (Jackson) or @SerializedName (Gson).

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.