Loading XML to Java Converter...
Please wait a moment

How to Convert XML to Java Classes - Step by Step Guide

Step 1

Paste or Upload Your XML

Start with the XML you want to read in Java: a config file, an API response or a sample message. One representative document is enough, but the more copies of a repeated element it has, the better the types get.

Example: Try This XML Data

Copy and paste this XML example to see how it works: ☕

<?xml version="1.0" encoding="UTF-8"?>
<company>
  <employee id="E001">
    <firstName>Rajesh</firstName>
    <salary>95000</salary>
    <isActive>true</isActive>
    <address>
      <city>Bangalore</city>
      <zipCode>560001</zipCode>
    </address>
  </employee>
</company>
Step 2

Let It Read the Structure

There is nothing to set up. The output is plain Java classes wired for JAXB, and it updates as you type:

•
JAXB annotations: every field carries the original XML name in a JAXB annotation
•
Getters and setters: private fields with a getter and setter each
•
Nested classes: each element with children or attributes gets a static nested class; repeats become List<T>
•
Types from the values: Integer, Long, Double and Boolean only when every value fits; everything else is String
Step 3

Check the Generated Classes

This is exactly what the example above turns into. The top element becomes Root, salary and zipCode become Integer and isActive becomes Boolean.

Java Code Output

Your XML becomes these Java classes:

// Save as Root.java. Java 8 has JAXB built in; on Java 11+ add jakarta.xml.bind-api
// and jaxb-runtime 2.3.x. On Jakarta EE 9+ or Spring Boot 3, change javax.xml.bind to jakarta.xml.bind.
// Read a document with: Root data = (Root) JAXBContext.newInstance(Root.class)
//     .createUnmarshaller().unmarshal(new StringReader(xml));

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "company")
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

    @XmlElement(name = "employee")
    private Employee employee;

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    public static class Employee {

        @XmlAttribute(name = "id")
        private String id;

        @XmlElement(name = "firstName")
        private String firstName;

        @XmlElement(name = "salary")
        private Integer salary;

        @XmlElement(name = "isActive")
        private Boolean isActive;

        @XmlElement(name = "address")
        private Address address;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public Integer getSalary() {
            return salary;
        }

        public void setSalary(Integer salary) {
            this.salary = salary;
        }

        public Boolean getIsActive() {
            return isActive;
        }

        public void setIsActive(Boolean isActive) {
            this.isActive = isActive;
        }

        public Address getAddress() {
            return address;
        }

        public void setAddress(Address address) {
            this.address = address;
        }
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    public static class Address {

        @XmlElement(name = "city")
        private String city;

        @XmlElement(name = "zipCode")
        private Integer zipCode;

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public Integer getZipCode() {
            return zipCode;
        }

        public void setZipCode(Integer zipCode) {
            this.zipCode = zipCode;
        }
    }
}
Step 4

Copy or Download the Code

Copy the code or download it as Root.java, add it to your project and read documents with a JAXB Unmarshaller.

•
Copy to clipboard and paste into an existing file
•
Download Root.java and add it to your project
•
Use with frameworks like Spring Boot and JAX-RS that read XML through JAXB
•
Rename freely: add a package line or rename Root; the XML names live in the annotations

What is XML to Java Conversion?

XML to Java conversion means writing the Java classes an XML document maps onto, so JAXB can unmarshal the document into typed objects instead of you walking DOM nodes by hand. Writing those classes by hand gets tedious fast for anything with more than a few elements.

The generated classes keep the exact XML names in their annotations, so the Java side can use normal camelCase names and still match your documents, and the same classes can marshal the XML back out.

Frequently Asked Questions

What Java annotations are included in the generated classes?

The standard JAXB ones: @XmlRootElement on the top class, @XmlAccessorType(XmlAccessType.FIELD) on every class, @XmlElement and @XmlAttribute on fields, and @XmlValue where an element has attributes and a text value. Each annotation carries the original XML name, so the Java fields can use normal camelCase names. Everything lands in one Root.java: Root is the public class and every other class is a public static class inside it.

How are the field types chosen?

From every value the element or attribute has anywhere in the document. Whole numbers become Integer, or Long once they no longer fit in 32 bits, so an ID like 8901240544102066246 is a Long and keeps every digit; anything past 64 bits stays a String. Decimals become Double, which means 14.50 is read as 14.5. Only lowercase true and false become Boolean. Values like 00501, hex such as 0x1F, and elements that are sometimes empty stay String, because JAXB would drop the zeros or fail on an empty number.

How does the tool handle repeated and nested elements?

Every element that has children or attributes gets its own class, and all copies of a repeated element are merged into it, so a child that only shows up in the second <subscriber> is still a field. Repeated elements become a List<T> that starts out empty. An element like <price currency="USD">10.50</price> gets a class with a currency attribute field and a value field for the text.

Does it handle XML namespaces?

Yes. When an element or attribute belongs to a namespace, its annotation states it with namespace = "...", so prefixed documents like <ns:doc xmlns:ns="urn:x"> and default xmlns documents read back correctly. The xmlns declarations themselves are not turned into fields.

What Java versions and frameworks can use these classes?

Java 8 ships JAXB, so the file compiles as it is. From Java 11 on JAXB is a separate library: add jakarta.xml.bind-api and jaxb-runtime 2.3.x to your build. Spring Boot 3 and Jakarta EE 9+ use the jakarta.xml.bind package instead of javax.xml.bind, so change the import lines there. The classes are plain JAXB, so Spring's JAXB message converter and JAX-RS providers that use JAXB can read and write them too.

Is my XML uploaded anywhere?

No. The conversion runs in your browser and the XML never leaves your machine, so the only practical limit on file size is your browser's memory.