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 XML into XML Input or Upload a file. Repeated elements must appear twice to become a list. Sample loads an order:

<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>

Parse errors appear in the output panel. The XML Validator shows the line and column.

2

Set package and accessors

Root class renames the top-level class (empty uses the root element name); the download is named after it so the file name matches the public class. Package sets the package line. Getters & setters makes fields private and adds 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 get @XmlAttribute. If an element has attributes and text, such as <price currency="USD">24.99</price>, the text goes into a field annotated @XmlValue.

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?

From your sample values: whole numbers become int or long, decimals double, true/false boolean. Values with a leading zero stay String. Optional fields use boxed types like Integer.

Is the XML uploaded?

No. It is parsed in your browser. Inputs over 256 KB are rejected.

Related tools

Other converters you might reach for next: