XML Input

Java Output

What the generator writes

Paste an XML sample and you get a single Java file: the root class carries @XmlRootElement, and every other element becomes a public static nested class, so the file compiles as one unit. Fields are annotated with Jakarta XML Binding (JAXB) annotations: @XmlAttribute, @XmlElement and @XmlValue, each with the exact XML name.

Repeated elements become List<T> initialised to an ArrayList. Required numbers use primitives (int, long, double, boolean) and optional ones use the boxed types so they can be null. Namespaced elements get a namespace = "..." argument, following the XML Namespaces URIs in your sample.

Imports use jakarta.xml.bind.annotation. On a Java 8 / Java EE stack, change them to javax.xml.bind.annotation. You also need a JAXB runtime such as the Eclipse JAXB implementation, since the JDK stopped shipping one in Java 11. We unmarshalled the page sample with JAXB 4.0 on Java 21 before shipping this. The XML is processed in your browser, not uploaded.

How to use it

Paste, set the package, copy.

1

Paste a sample

Paste the XML into XML Input, or use Upload for a file. Any child that occurs twice under one parent is declared as List<T> with = new ArrayList<>(), so make sure your sample repeats it. Sample loads an order with two items:

<order id="ORD-7731" status="paid">
  <item sku="SKU-101"><qty>2</qty><price currency="USD">24.99</price></item>
  <item sku="SKU-205"><qty>1</qty><price currency="USD">39.50</price></item>
</order>

Malformed XML puts the parser error in the Java panel rather than half a class. When the cause is not obvious, the XML Validator gives you the position of the first error.

2

Set package and accessors

Root class sets the outer class name; if blank, <order> becomes Order. The download takes that class name (Order.java), and every other type sits inside it as a public static nested class. Package sets the package line. Getters & setters keeps fields private with accessors; untick it for public fields.

3

Unmarshal

Download the .java file, then Order order = (Order) JAXBContext.newInstance(Order.class).createUnmarshaller().unmarshal(file);.

Where it helps

SOAP and B2B integrations

When you have a sample message but no XSD, generated JAXB classes are a fast way to start unmarshalling.

Spring Boot services

Spring's Jaxb2Marshaller works with annotated classes like these for XML request and response bodies.

Batch imports

Read supplier or banking files into objects in a Spring Batch job instead of walking a DOM by hand.

Testing

Unmarshal fixtures and assert on fields, which is far less brittle than comparing XML strings.

Common questions

Which JAXB dependency do I need?

The jakarta.xml.bind-api API plus a runtime such as org.glassfish.jaxb:jaxb-runtime. Since Java 11 the JDK does not include JAXB, see the Eclipse JAXB project.

Why are the other classes nested?

A Java file can only hold one public top-level class. Making the rest public static nested classes keeps everything in one file that compiles as is, and JAXB handles static nested classes fine.

How are attributes and text handled?

Attributes are annotated @XmlAttribute(name = "...") and children @XmlElement(name = "..."). When an element carries an attribute and text, as <price currency="USD">39.50</price> does in the sample, it gets its own class with the text in a field marked @XmlValue. Text mixed in between child elements is left out with a comment, since JAXB would need @XmlMixed for it.

What about mixed content?

JAXB does not allow @XmlValue next to @XmlElement fields, so text mixed in between child elements is left out with a comment. Use @XmlMixed by hand if you need it.

How are types chosen?

Read off the sample: qty values of 2 and 1 give int, anything past the 32-bit range is long, 39.50 is double, and a true/false field is boolean. Fields some records omit, plus @XmlValue text, use boxed types such as Integer or Double so they can hold null. A zero-padded value stays String.

Is the XML uploaded?

No. The JAXB classes are built by JavaScript in your browser tab, and the XML is not sent to any server. The input limit is 256 KB; an order with two or three items is plenty for the generator to see every field.

Related tools

Other converters you might reach for next: