Parsing XML files with JAXB

See the source image

Java Architecture for XML Binding (JAXB)

JAXB is a framework that can allows us java developers to map our java classes to xml representations. JAXB is known for two key features . One would be the ability to marshal java objects into XML , and secondly is the ability to unmarshall the those XML back into java objects.

JAXB , in other words is making it very easy to us to store, and retrieve data in memory in xml format. Without the actual need to implement a specific set of XML load/save routines depending on the programs class structure.

JAXB is particularly useful when the specification is complex and changing.

JAXB is a part of the Java SE platform and one of the APIs in the Java EE platform, and is part of the Java Web Services Development Pack (JWSDP).

JAXB 2 Tutorial

Let’s see the steps to convert java object into XML document.

  • Create POJO or bind the schema and generate the classes
  • Create the JAXBContext object
  • Create the Marshaller objects
  • Create the content tree by using set methods
  • Call the marshal method

@XmlRootElement specifies the root element for the xml document.

@XmlAttribute specifies the attribute for the root element.

@XmlElement specifies the sub element for the root element.

See the source image

Citation : David Gassner Creating and parsing with JAXB, https://www.oracle.com/technetwork/articles/javase/index-140168.html, https://www.javatpoint.com/jaxb-tutorial , https://en.wikipedia.org/wiki/Java_Architecture_for_XML_Binding

Leave a comment