Advertisement728 × 90 — Leaderboard
Advertisement320 × 50 — Mobile banner

XML to Java Class

Generate Java POJOs or Records from XML
Session restored from your last visit
Also need:XML to JSONJSON to JavaXML Formatter
Advertisement — Fullscreen
Input
Output
Input
AdvertisementResponsive
Output
Ready0 chars0 lines
Advertisement728 × 90

About XML to Java Class

Paste your XML and get Java class definitions. Supports POJOs (Java 8/11), Records (Java 17/21), and optional JAXB, Jackson, or Gson annotations.

Frequently Asked Questions

What is XML to Java class generation?
XML to Java class generation analyses an XML document and produces Java class definitions with JAXB annotations. Each XML element becomes a class with @XmlElement fields and each attribute becomes an @XmlAttribute field, ready for JAXBContext.createUnmarshaller() to unmarshal the XML.
What is the difference between JAXB, DOM, SAX, and StAX for XML in Java?
DOM loads the entire document into a tree (good for small files, random access). SAX is event-driven and memory-efficient (good for large files, one-pass processing). StAX is a pull-based event model (more control than SAX, less memory than DOM). JAXB maps XML directly to annotated Java objects (best for schema-defined XML and most web service use cases).
How do I use the generated Java class with JAXB for XML unmarshalling?
Add @XmlRootElement to the class and @XmlAttribute to attribute fields. Then: JAXBContext ctx = JAXBContext.newInstance(YourClass.class); Unmarshaller u = ctx.createUnmarshaller(); YourClass obj = (YourClass) u.unmarshal(new File("data.xml")). JAXB is included in Java 8 and earlier; for Java 11+ add the jaxb-ri dependency.
How are XML attributes different from child elements in the generated Java class?
The generator annotates attribute fields with @XmlAttribute and element fields with @XmlElement. In the parsed document, attributes are defined inside the opening tag (<user id="1">) while child elements are nested tags (<name>Alice</name>). The distinction matters for both reading and writing XML correctly with JAXB.
How do I handle repeated XML child elements (arrays) with JAXB?
For a repeated element <item>, declare a List<Item> field annotated with @XmlElement(name = "item"). JAXB will populate the list with one object per <item> element. For @XmlElementWrapper, add a wrapper element around the list: @XmlElementWrapper(name = "items") @XmlElement(name = "item") List<Item> items.
Advertisement300 × 250
Advertisement300 × 250
Ad
AdvertisementSticky banner
Ad
AdvertisementSticky banner