Why generate Java classes from an XML sample?
Java's XML world is class-first: JAXB, Jackson XML and
their friends all want a model class per element before
they will read a document, and writing those classes means
transcribing somebody else's format — RSS feeds, SOAP
responses, configuration dialects, export files — into
fields with the right names, the right nullability and the
right collection types. The sample document already
contains every one of those facts: which elements repeat,
which attributes come and go, which leaf values look
numeric, which element is missing from half the records.
This converter reads them off and writes the classes —
plain public-field classes by default, or annotated for
jakarta.xml.bind if you tick the box — and,
because a sample is evidence rather than a contract, it
reports every judgement it made along the way.
How to use this tool
- Paste the XML, upload an .xml file, or drop one on the panel. The document must be well-formed; malformed input is reported with a line and column, not converted.
- Optionally name the root class; leave it blank to take the root element's name. Decide on type inference — leave it on for samples you trust, off if the values are identifiers that merely look numeric.
- Tick the JAXB box if you want annotations; leave it off for plain classes you will wire to your own mapping.
- Press Generate Java classes. The root class comes first.
- Read the notes. Boxed fields, mixed content and skipped comments or processing instructions are all listed there — review them before you rely on the result.
The mapping rules
Elements. Each distinct element shape
becomes a class named after the element, PascalCased;
identical shapes share one class. A child that repeats
anywhere becomes a List; a sometimes-absent
child keeps its single type, boxed if it is a primitive.
Attributes become fields in their
element's class, marked as attributes. Text.
A leaf element's text types its field — integer-looking
text is int or long, decimal is
double, true/false is boolean,
mixed evidence falls back to String with a
note — and text alongside children or attributes collects
into a value field. Not data:
comments, processing instructions and the DOCTYPE are
skipped and counted in the notes; CDATA is read as text.
Entities are decoded before inference, exactly as a real
parser would hand them to you.
Worked example
Given this catalogue (the second book has a note the first lacks, and only the first has stock):
<catalog count="2">
<book id="bk101" lang="en">
<title>XML Guide</title>
<price>44.95</price>
<stock>12</stock>
</book>
<book id="bk102">
<title>Java in Practice</title>
<price>39.99</price>
<note>Ships free</note>
</book>
</catalog> the converter emits:
import java.util.List;
public class Catalog {
public int count; // attribute
public List<Book> book;
}
public class Book {
public String id; // attribute
public String lang; // attribute
public String title;
public double price;
public Integer stock;
public String note;
} stock is Integer, not
int, because the second book proves it can be
absent — and a plain int would make “missing”
indistinguishable from “zero in stock”. That distinction
is the sample's own evidence, written where Java needs it.