araç köşesi

XML to Java Class (JAXB)

Generate JAXB-annotated Java classes from sample XML; attributes become @XmlAttribute, repeated elements become List<>

Your data stays with you. Conversion happens inside the browser; nothing is sent to a server.

How it works

To read a SOAP response or an XML export as objects in Java, JAXB needs classes that match the XML. Paste a sample into the left pane; the right pane shows one class per element. The root element gets @XmlRootElement(name = "order"), and nested elements are written inside the root class as public static class, so the output compiles as a single Order.java file. Attributes such as id="1024" become @XmlAttribute fields, child elements become @XmlElement fields. When an element has both attributes and text (<item sku="US-11">Steel pot</item>), the text goes into an @XmlValue field. If the same element appears twice under one parent, the field becomes List<Item>. Value types are read from the text: 1024 is int, 59.50 is double, true is boolean. A value with a leading zero such as the ZIP code 02134 stays String, because as a number it would lose the zero. An attribute or child that some elements lack gets a wrapper type like Integer so it can be null; in the sample, the second item has no qty, so qty is Integer. Choose jakarta for Spring Boot 3 and Jakarta EE, javax for Java 8 projects.

This tool is also known as xml to java, xml to java class, xml to pojo, jaxb class generator, xml to java converter.

What is JAXB?

JAXB (Jakarta XML Binding) is the Java standard for mapping objects to XML and back. Annotations such as @XmlRootElement, @XmlElement and @XmlAttribute tell it which field belongs to which part of the XML; an Unmarshaller reads XML into objects and a Marshaller writes objects out as XML. It is no longer part of the JDK since Java 11, so you add it to your project as a dependency.

What is XML attribute vs. element?

XML can carry a value in two ways: as an element (<total>59.50</total>) or as an attribute (<order id="1024">). An attribute holds a single text value and cannot repeat on the same element; an element can hold child elements and can appear many times. JAXB keeps the two apart with @XmlAttribute and @XmlElement, and a field with the wrong annotation is read as empty.

What is the difference between JAXB and Jackson XML?

JAXB is the Java standard for XML and works with annotations such as @XmlElement; SOAP clients, classes generated from an XSD with xjc and Jakarta EE servers expect it. Jackson XML (jackson-dataformat-xml) is the XML add-on for Jackson, the library most projects already use for JSON; it has its own annotations like @JacksonXmlProperty and can also read JAXB annotations through a module. For a SOAP or XSD-based project use JAXB; for a REST project that already handles JSON with Jackson, Jackson XML adds fewer dependencies.

Which part of the XML becomes which JAXB annotation

JAXB matches XML to Java fields through annotations. The tool puts the original XML name on every annotation, so reading works even when the Java field name differs. Each class carries @XmlAccessorType(XmlAccessType.FIELD): JAXB reads the fields directly, and the getters and setters are only there for your own code.

The list decision is not made per parent. If one order has a single item and another has three, item is List<Item> in both places. Otherwise a document with one item would not fit the field type when it is read.

  • <order id="1024"> → @XmlAttribute(name = "id") private int id;
  • <total>59.50</total> → @XmlElement(name = "total") private double total;
  • <item sku="US-11">Steel pot</item> → class Item: @XmlAttribute sku + @XmlValue value
  • two <item> elements → @XmlElement(name = "item") private List<Item> item;

Honest limits: namespaces, mixed content, dates

Namespaces (xmlns) are not resolved: the prefix of <cbc:ID> is dropped and the field is annotated with the plain name "ID". If your document uses namespaces, add namespace = "..." to the annotations or an @XmlSchema in package-info.java by hand. For large schemas such as UBL invoices, generating classes from the XSD with xjc gives a more accurate result.

In an element that mixes text and child elements (<note>Pay on <b>delivery</b></note>) the text is skipped and a comment is written at the top of the class, because JAXB does not allow @XmlValue next to @XmlElement in one class. Date strings (2026-09-27) are not turned into LocalDate; they stay String. Types are guessed from one sample, so the more variation your sample shows, the more accurate the classes.

Frequently asked questions

How do I generate a Java class from XML?

Paste sample XML into the left pane on this page and type a package name. The right pane shows Java classes with the JAXB annotations in place. Copy or download the code; the file carries the root class name and can go into your project as it is.

Should I choose jakarta or javax?

JAXB was removed from the JDK in Java 11, and with Jakarta EE 9 its package was renamed to jakarta.xml.bind. If you use Spring Boot 3 or a current Jakarta EE server, choose jakarta. On Java 8 or an older project with Spring Boot 2, choose javax. The wrong package gives a compile error.

Why did the ZIP code become String instead of int?

A value with a leading zero, such as the ZIP code 02134 or a phone number, loses that zero when read as a number. The tool therefore keeps values that start with zero, and numbers longer than 18 digits, as String. If you want every field as text, set Numbers and true/false values to All String.

Is the XML I paste sent to a server?

No. Reading the XML, inferring the types and writing the Java code all happen in your browser, so even a service response with customer data never leaves this page.