JAXB for beginners: Tutorial for XML processing with Java
Related Videos: Writing and Reading XML with JAXB (May 2024).
In order for applications to communicate with each other, it is often necessary to convert the data into a format that is highly compatible. For this purpose, the JAXB library exists under Java, which processes objects into XML files.
Generate an XML file with JAXB - Best Practices
JAXB stands for Java Architecture for XML Binding . It enables Java objects to be converted to XML files and vice versa. Before you get started, there are a few points to keep in mind:
- JAXB has been an integral part of the JRE suite since version 1.6 . In addition, the Java development environment should be up to date because it fixed bugs.
- Avoid primitive data types such as float, decimal or negative integer as well as anonymous data types . There is no equivalent in JAXB for this. Otherwise, objects, numeric data types, lists and sets can be converted. JAXB also cannot handle complex data types such as java.time.LocalDate . This requires a specially created adapter .
- In technical jargon, the direction from Java to XML is called marshaling, while unmarshaling stands for the opposite direction.
XML annotations
Latest videos
JAXB for beginners: tutorial using the example of a CD collection
AudioCD.java: The attributes of the audio CD are defined here
MusicDB.java: The music collection consists of a list of audio CDs
Our tutorial is about a CD collection that is output in XML. It consists of several audio CDs. The artist, the year of release, the name of the album and the songs are recorded for each audio CD.
- First the class should be created, which contains the data of an audio CD. This includes the artist, the year of release, the album and the songs that are on the CD.
- In order to better control the output in XML format, XML annotations are required that begin with @ under Java.
- The root element in the XML file can be specified using the following annotation: @XmlRootElement (name = "Audiocd")
- You can insert an annotation above the getter or setter methods that specifies an alternative name for a Java attribute. For example, instead of the attribute year, the year of publication should be output: @XmlElement (name = "year of publication")
- The order of the attributes is determined by the annotation @XmlType (propOrder = {"artist", "title", "year", "songlist"}) . This means that the name of the artist appears first in the XML file, followed by the name of the album, the year of release and the list with the songs. Otherwise the order depends on the Java file.
- Next, create a new Java class called MusicDB, which contains a list of audio CDs. The name of the music collection and the location of the music collection are also recorded here. In principle, the procedure is similar to that in the Java file mentioned above.
From Java objects to XML files
Latest videos
AudioCDMain.java: Several audio CDs are created here
AudioCDMain.java: Create the music collection
AudioCDMain.java: Converts the code to XML
The conversion itself takes place in a test program, more precisely in the main method.
- First, create multiple Audio CD instances so that multiple Audio CDs are created.
- An instance of the MusicDB type music collection is also required.
- In addition, JAXB needs to know which Java class contains the root element. In our case this is the MusicDB class: JAXBContext context = JAXBContext.newInstance (MusicDB.class);
- The conversion takes place using the Marshaller instance, which is linked with the JAXB method mentioned above: Marshaller m = context.createMarshaller ();
- The Marshaller method provides further options, such as that the XML document is output formatted: .setProperty (Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
The whole thing in the opposite direction
Latest videos
AudioCDMain.java: JAXB generates Java objects from an XML file
The XML file has already been formatted by JAXB
Basically, the conversion in the other direction proceeds in a similar way:
- First, an instance of the unmarshaller method is created: unmarshaller at = context.createUnmarshaller ();
- Then the unmarshaler instance needs the name of the XML file previously created: MusicDB mdb2 = (MusicDB) um.unmarshal (new FileReader (MUSICDB_XML));