XML to POJO Converter - Generate Plain Java Classes from XML Online
Convert XML data to plain Java classes with getters, setters and toString, with no annotations or libraries.
XML to POJO converter, Java POJOs, Java classes, Java code generation
How to Convert XML to Java POJOs - Step by Step Guide
Paste or Upload Your XML
Start with a document that looks like the data you will handle: an API response, a config file or a sample message. The more copies of a repeated element it has, the better the field 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>John</firstName>
<age>30</age>
<salary>75000.50</salary>
<active>true</active>
<department>
<name>Engineering</name>
<budget>1000000</budget>
</department>
</employee>
</company>Let It Read the Structure
There is nothing to set up. The output is plain Java classes and it updates as you type:
Check the Generated Classes
This is exactly what the example above turns into. The top element becomes Root, age and budget become Integer, salary becomes Double and active becomes Boolean. More background on the idea: plain old Java objects.
Java Code Output
Your XML becomes these Java POJOs:
// Save as Root.java. Plain Java with no annotations or libraries, so it compiles on
// Java 8 and later. Filling the objects from XML is up to your own parsing code.
// <company>
public class Root {
private Employee employee; // <employee>
public Root() {
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
@Override
public String toString() {
return "Root{" +
"employee=" + employee +
"}";
}
// <employee>
public static class Employee {
private String id; // attribute id
private String firstName; // <firstName>
private Integer age; // <age>
private Double salary; // <salary>
private Boolean active; // <active>
private Department department; // <department>
public Employee() {
}
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 getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public Boolean getActive() {
return active;
}
public void setActive(Boolean active) {
this.active = active;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", firstName=" + firstName +
", age=" + age +
", salary=" + salary +
", active=" + active +
", department=" + department +
"}";
}
}
// <department>
public static class Department {
private String name; // <name>
private Integer budget; // <budget>
public Department() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getBudget() {
return budget;
}
public void setBudget(Integer budget) {
this.budget = budget;
}
@Override
public String toString() {
return "Department{" +
"name=" + name +
", budget=" + budget +
"}";
}
}
}Copy or Download the Code
Copy the code or download it as Root.java, add a package line if you need one and drop it into your project.
What is XML to POJO Conversion?
XML to POJO conversion means writing the plain Java classes that match the shape of an XML document: one class per element that holds other elements or attributes, one field per value. Doing that by hand gets tedious once a document has more than a handful of elements.
The result is ordinary JavaBean code with private fields and public accessors. It has no tie to any XML library, so you can keep it in a module that has no dependencies and fill it from whatever parser you already use.
Frequently Asked Questions
What exactly is in the generated POJOs?
Plain Java and nothing else: a private field for every attribute and child element, a public no-argument constructor, a getter and setter for each field and a toString that lists the field values. There are no annotations and no imports beyond java.util.List and ArrayList when a list is needed. Everything lands in one Root.java: Root is the public class and every other class is a public static class inside it, so the file compiles on its own.
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; anything past 64 bits stays a String. Decimals become Double, so 14.50 is stored 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, so you never lose leading zeros by parsing them into a number.
What happens with 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 <project> is still a field. Repeated elements become a List<T> that starts out empty. An element like <salary currency="USD">95000.00</salary> becomes a small class with a currency field and a value field for the text.
Can these classes read my XML file on their own?
No. A POJO is just a data holder, so filling it from XML is up to your code, for example a DOM or StAX parser that calls the setters. Each field has a comment with the XML name it came from to make that mapping easy. If you would rather have the parsing done for you, the XML to Java converter writes the same kind of classes with JAXB annotations that an Unmarshaller reads straight back.
Do the classes work with Spring and other Java frameworks?
They follow JavaBean conventions (no-argument constructor, getX and setX methods), which is what Spring, Jackson and most mapping libraries expect from a DTO. They carry no JPA or Hibernate annotations, so turning them into database entities means adding those yourself.
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.