By default when marshaling to xml file, all properties are marshaled as elements. This default behaviour can be changed and this post explains how to do it. In this post I will marshall the below java bean class, and id property as its attribute. Country 1 package package1; 2 3 import javax.xml.bind.annotation.XmlAttribute; 4 import javax.xml.bind.annotation.XmlRootElement;…… Continue reading Marshaling a property as xml attribute
Category: JAXB
Preventing a property from being marshalled to xml file
By default all properties of an instance are marshalled as elements in an xml file. As shown below.. Marshalling an Country insance with below class structure Country package package2; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Country { private int id; private String name; private int population; public int getId() { return id; } public void setId(int…… Continue reading Preventing a property from being marshalled to xml file
Listening to unmarshalling events
By subclassing Unmarshaller.Listener abstract class, we can create listener class which will listen to unmarshling events. We can add our code if we want to perform some actions before and after unmarshalling. The Unmarshaller.Listener class has two methods which can be overridden. By default the methods doesn’t perform any actions. 1) public void beforeUnmarshal(Object target,…… Continue reading Listening to unmarshalling events
Listening to marshaling events
By subclassing Marshaller.Listener abstract class, we can create listener class which will listen to marshling events. We can add our code if we want to perform some actions before and after marshalling. The Marshaller.Listener class has two methods which can be overridden. By default the methods doesn’t perform any actions. 1) public void beforeMarshal(Object source)…… Continue reading Listening to marshaling events
Programmatically generating schema for java classes
This post will explain how to generate schema files for java classes programmatically. The schema is generated by an instance of JAXBContext. In addition to JAXBContext we also need SchemaOutputResolver for this purpose. The SchemaOutputResolver class is an abstract class so we need to create a new class extending SchemaOutputResolver as shown below. package JAXB;…… Continue reading Programmatically generating schema for java classes
Updating a particular element in JAXB generated xml
Sometimes xml documents are very big and our requirement is to update a particular section of the document. JAXB binder comes to our help. Binder object maintains the mapping between the java object and xml infoset like DOM. Any modification to java object can be synchronized to xml held by binder object and vice versa.…… Continue reading Updating a particular element in JAXB generated xml
Marshalling and Unmarshalling java class
1) First we need to create jaxb context as shown below. A instance of this class is always required, as it is through this class we get marshaller and unmarshaller objects JAXBContext jaxbContext = JAXBContext.newInstance(Country.class); An instance of JAXBContext is thread safe but it is heavy weight object. So it is recommended to reuse the…… Continue reading Marshalling and Unmarshalling java class