JAXB Parsing. XSD. XML Namespaces.

We cannot ignore the JAXB XML Parser. Let’s explore its functionality by considering examples of manual class description and auto generation of classes.

Parsing XML with JAXB. Describe classes in several ways. Work through the example with @XmlElementWrapper, with multi namespace.



Working with XML is a common task, and solutions that enable quick problem-solving are of great interest to the developer community. When it comes to XML, one popular approach is using JAXB (Java Architecture for XML Binding) to handle XML data in Java.

The basic principle of using JAXB involves the following steps:

  1. Obtain or generate an XML document.
  2. Create an XSD schema based on the XML document (two options: manual or auto-generation).
  3. Generate JAXB classes that describe the elements in the XML based on the XSD schema, or manually write the classes.
  4. Use the JAXB unmarshaller to parse the XML and convert it into Java objects.

To generate XSD you can use online generators, for example Freeformatter.


When working with XML in IntelliJ IDEA, you can utilize the WebServices plugin to generate JAXB classes based on an XSD schema. This plugin simplifies the process of creating the necessary classes for working with XML data.

Code example: Unmarshaller XML.

//Start Unmarshalling
JAXBContext jaxbContext = JAXBContext.newInstance(ActorCast.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
ActorCast jaxbElement = (ActorCast) unmarshaller.unmarshal(new File(XMLFILE));
//Stop Unmarshalling
List cast = jaxbElement.getCast();
for(ActorCast.Cast cst : cast){
	System.out.println("Actor: " +cst.getActor().getFirstName()+" "+cst.getActor().getLastName()+", "+cst.getActor().getBirthDay());
	System.out.println("Character: " +cst.getCharacter().getFirstName()+" "+cst.getCharacter().getLastName());

To understand the process, let us now describe the XML model manually. So there is XML:

According to the specification, XML our contains:
Root element– ActorCast. It can only be 1 in a document.
XML prolog

<!--?xml version="1.0" encoding="UTF-8" standalone="no"?-->

XML Elements – Cast, Character,FirstName …
XML Attribute – id. The value of attributes must always be enclosed in quotes. And the Value of the id attribute is 1.

For each XML node in JAXB, there is a corresponding annotation.
Root element @XmlRootElement
XML Elements @XmlElement
XML Attribute @XmlAttribute

XML Tree

Added a few annotations:
@XmlAccessorType(XmlAccessType.FIELD) Access directly to class fields.
@Getter, @Setter, @ToString Annotations from lombok library, code is more readable, standard getters and setters.

Another useful annotation was not used: @XmlElementWrapper Indicates that, the element is a wrapper, in our case it is essentially a Cast element.

Often, working XMLs don’t always look so friendly and have a simple structure, as in the Actors example. We add a namespace – XML Namespaces. The main purpose of Namespaces is to avoid element naming conflicts.

Thus ch area elements are out of scope in ac area and vice versa.
JAXB all annotations have the namespace parameter
@XmlRootElement(name = “ActorCast”, namespace = “http://www.gotoqa.ru/character/”)

In conclusion, JAXB (Java Architecture for XML Binding) is a powerful tool for working with XML in Java, offering developers a flexible and efficient solution for handling XML data. Whether manually describing classes or generating them automatically, JAXB simplifies the process of parsing XML and converting it into Java objects. With its rich set of annotations and features, JAXB enables seamless integration of XML processing into Java applications.

Link to the full version of the project on GitHub:
Github JAXBParsingProject

Related Posts