XML to Java POJO Converter

How the converter works

Paste any well-formed XML document and the converter does four things:

  1. Parses the XML with the browser's built-in DOMParser.
  2. Walks the tree and groups child elements by tag name.
  3. Infers types: integers, longs, doubles, booleans, ISO 8601 dates and datetimes are detected; everything else falls back to String.
  4. Emits Java classes β€” one per nested XML element β€” with the annotations you chose.

Repeating elements become List<T>. XML attributes become Java fields tagged with the appropriate attribute annotation. Everything runs client-side: no upload, no tracking, no backend call.

Which output style should I pick?

JAXB (default)

The Jakarta XML Binding API is the Jakarta EE standard for mapping XML to Java. It uses @XmlRootElement, @XmlAttribute, @XmlElement, and @XmlAccessorType. Pick this when:

  • You work on a Jakarta EE / Spring Boot project that already uses the jakarta.xml.bind API.
  • You need to generate XML from Java objects with Marshaller.
  • You want well-documented, stable annotations that play nice with every IDE.
// Unmarshal with JAXB
JAXBContext ctx = JAXBContext.newInstance(User.class);
Unmarshaller u = ctx.createUnmarshaller();
User user = (User) u.unmarshal(new StringReader(xml));

Jackson XML

Jackson can parse XML with the same ObjectMapper you use for JSON. This is valuable when your service handles both formats β€” one mental model, one dependency, no duplicate mapping code.

XmlMapper xmlMapper = new XmlMapper();
User user = xmlMapper.readValue(xml, User.class);

// And back to XML
String out = xmlMapper.writeValueAsString(user);

Requires the jackson-dataformat-xml dependency.

Plain POJO

No annotations at all. Useful when you want to do the parsing manually with DOM, SAX or StAX and retain full control, or when you'll layer your own custom annotations on top.

How type inference works

XML value exampleInferred Java type
42, -17int
10000000000 (> Integer.MAX_VALUE)long
3.14, -2.5double
true, falseboolean
2024-03-15LocalDate
2024-03-15T10:30:00ZInstant
Anything elseString

Common XML patterns and their Java shape

Attributes

<book isbn="978-0-321-35668-0" lang="en">
  <title>Effective Java</title>
</book>

Produces fields String isbn, String lang, String title with @XmlAttribute annotations for the first two.

Repeating child elements

<library>
  <book>...</book>
  <book>...</book>
  <book>...</book>
</library>

Generates List<Book> book (or @XmlElementWrapper + @XmlElement if you want a wrapping tag).

Mixed content (text + elements)

<p>Some <b>bold</b> text</p>

Mixed content is rare and hard to model cleanly. The converter treats it as a child-bearing element; adjust the output manually with @XmlMixed if you really need it.

Parsing and serializing with JAXB

Full round-trip using the generated class:

import jakarta.xml.bind.*;
import java.io.*;

// XML β†’ Java object
JAXBContext ctx = JAXBContext.newInstance(User.class);
Unmarshaller u  = ctx.createUnmarshaller();
User user = (User) u.unmarshal(new File("user.xml"));

// Java object β†’ XML
Marshaller m = ctx.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(user, new File("user-output.xml"));

Parsing with Jackson XML

import com.fasterxml.jackson.dataformat.xml.XmlMapper;

XmlMapper xml = new XmlMapper();
User user = xml.readValue(new File("user.xml"), User.class);

// Back to XML
String out = xml.writerWithDefaultPrettyPrinter().writeValueAsString(user);

Maven:

<dependency>
  <groupId>com.fasterxml.jackson.dataformat</groupId>
  <artifactId>jackson-dataformat-xml</artifactId>
  <version>2.18.2</version>
</dependency>

XSD vs XML sample generation

This tool infers classes from a sample XML document. That's fast and works on documents you already have, but it can't know about optional fields, documentation, default values, or strict type constraints.

When you do have an XSD schema, generate classes from it with the JDK's xjc tool:

xjc -d src/main/java -p com.example.model schema.xsd

Or via Maven with jaxb2-maven-plugin. XSD-based generation is richer β€” it captures choice elements, enum restrictions, nillable fields and more.

Common mistakes

  • Using JAXB on Java 11+ without the dependency. JAXB was removed from the JDK in Java 11. Add jakarta.xml.bind-api + a runtime like glassfish.jaxb or sun-jaxb.
  • Mixing javax.xml.bind and jakarta.xml.bind. The Jakarta namespace replaces the old javax one since Jakarta EE 9. Don't combine them.
  • Missing no-arg constructor. JAXB requires it. Our generated classes get one implicitly since we don't define any constructor.
  • Expecting the sample converter to catch all nuances. An attribute count="0" in your sample will be inferred as int. If real payloads sometimes contain "" (empty) or null, switch to Integer manually.
  • Namespaces. The converter uses local tag names. For XML with namespaces you'll want to declare @XmlSchema on the package.