BufferedWriter out = new BufferedWriter(fstream);
try {
JAXBContext context = JAXBContext.newInstance(NarociloType.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(parameters, out);
out.newLine();
} catch (PropertyException pe) {
// TODO: Add catch code
pe.printStackTrace();
} catch (JAXBException jaxbe) {
// TODO: Add catch code
jaxbe.printStackTrace();
}
but empty types are not stored into XML. For example:
NarociloType.date = null
but i can not see in xml <date></date>.
JAXB marshalling does not create empty element for null values
Can i also change XML to object back with JAXBContext?
Note: The following example works in EclipseLink JAXB (MOXy), but not the JAXB reference implementation included in Java SE 6.
If you are using MOXy as your JAXB provider (I’m the tech lead), then you could use an
XmlAdapterfor this use case.DateAdatper
Root
Demo
Output
jaxb.properties
You use MOXy as your JAXB provider you need to provide a file called jaxb.properties in the same package as your domain classes with the following entry:
For More Information on XmlAdapter