Working with Jaxb in 2017
satya - 6/15/2017, 7:48:14 PM
Here is some old code I have borrowed
package com.ai.xml.jaxb20;
import java.io.File;
import java.util.*;
import javax.xml.bind.*;
public class XmlTest
{
private static JAXBContext jc = null;
public static void main(String[] args)
{
try
{
//Create a java object
Content c = createContent();
//Establish a jaxb context
System.out.println("Establish the context");
jc = JAXBContext.newInstance(c.getClass());
//jc = JAXBContext.newInstance("com.ai.xml.jaxb20");
//Get a marshaller
Marshaller m = jc.createMarshaller();
//Enable formatted xml output
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
//Marshal to system output: java to xml
m.marshal(c,System.out);
//Read xml into a java object
xmlToJava(jc);
}
catch (JAXBException x)
{
x.printStackTrace();
}
}//eof-main
public static void xmlToJava(JAXBContext jc)
throws JAXBException
{
//Get an unmarshaller
Unmarshaller um = jc.createUnmarshaller();
//Unmarshall the file into a content object
Object o = um.unmarshal(new File("E:\\satya\\webapps\\jaxb20\\xml\\content.xml"));
Content c = (Content)o;
//Remarshal it to system.out to see what you have read
Marshaller m = jc.createMarshaller();
m.marshal(c,System.out);
}
static public Content createContent()
{
Content c = new Content();
Folder f1 = new Folder("f1");
f1.addFile(new FileItem("f1","f1-de","f1-con"));
f1.addFile(new FileItem("f2","f2-de","f2-con"));
c.add(f1);
return c;
}
}//eof-class
satya - 6/15/2017, 7:48:44 PM
JAXBContext Java API
JAXBContext Java API
satya - 6/15/2017, 7:59:00 PM
Jaxb Marshaller API documentation
Jaxb Marshaller API documentation
satya - 6/15/2017, 8:04:09 PM
Marshaller.JAXB_FORMATED_OUTPUT
Marshaller.JAXB_FORMATED_OUTPUT
satya - 6/15/2017, 8:05:10 PM
You can find the supported properties here
satya - 6/15/2017, 8:10:31 PM
javax.xml.bind.annotation API
javax.xml.bind.annotation API
satya - 6/15/2017, 8:15:40 PM
What are key jaxb annotations
What are key jaxb annotations
satya - 6/15/2017, 8:39:56 PM
Some examples
@XmlRootElement
@XmlRootElement(name="myroot")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder={"field1","field5","field4"})
@XmlElement(name="myelem")
@XmlJavaTypeAdapter(MapAdapter.class)
@XmlAnyElement
satya - 6/15/2017, 8:43:01 PM
XmlAccessType.FIELD
XmlAccessType.FIELD
satya - 6/15/2017, 8:45:17 PM
Options are
FIELD: Every non static, non transient field in a JAXB-bound class will be automatically bound to XML, unless annotated by XmlTransient.
NONE: None of the fields or properties is bound to XML unless they are specifically annotated with some of the JAXB annotations.
PROPERTY: Every getter/setter pair in a JAXB-bound class will be automatically bound to XML, unless annotated by XmlTransient.
PUBLIC_MEMBER:Every public getter/setter pair and every public field will be automatically bound to XML, unless annotated by XmlTransient.
satya - 6/15/2017, 8:54:34 PM
XmlAnyElement in Jaxb
XmlAnyElement in Jaxb
satya - 6/15/2017, 9:01:58 PM
XmlAnyElement
It appears this annotation leaves a place holder for single DOM Element object, or an object, an array of objects, or a list of objects. All of them are open ended XML DOM trees. In my case I won't be using this much. I will leave it to future research.
satya - 6/15/2017, 9:02:24 PM
Looks like one needs a whole guide to unravel all the possibilities!!!
Looks like one needs a whole guide to unravel all the possibilities!!!
satya - 6/16/2017, 1:01:22 PM
jaxb converting java Map object to XML annotations?
jaxb converting java Map object to XML annotations?
Search for: jaxb converting java Map object to XML annotations?
satya - 6/16/2017, 1:01:42 PM
JAXB and java.util.Map: a good article and sample code
satya - 6/16/2017, 1:04:09 PM
XmlAdapter - JAXB's Secret Weapon: another article, 2010
satya - 6/16/2017, 1:07:07 PM
@XmlJavaTypeAdapter
@XmlJavaTypeAdapter
satya - 6/16/2017, 2:14:25 PM
Briefly XmlJavaTypeAdapter is there to
To take a class that is not very good at mapping or not able to map and return a class that has a well known mechanism to map. For example you can convert a map to a list. The list classes have a good way to map to a list of objects. The mapper can figure out to go between a list and a map programmatically.
satya - 6/16/2017, 2:15:19 PM
Alternatively
Just start with 2 copies of your data structures. One for functionality and one for going between java and xml. This is not as bad as it sounds. In a way that is what xmladapter is doing on the fly.
satya - 6/18/2017, 10:02:21 AM
What is XmlStreamWriter in Java?
What is XmlStreamWriter in Java?
satya - 6/18/2017, 10:07:14 AM
Here is a good article on XmlStreamWriter
satya - 6/18/2017, 10:09:17 AM
So what do you use it for?
When you want to explicitly write XML files element by element, comment by comment, name space by name space etc.
satya - 6/18/2017, 10:29:02 AM
Universal Jaxb tester
Universal Jaxb tester
satya - 6/18/2017, 10:31:15 AM
It may look like this
//Get a tester that is type specific
JaxbTester<Your-Type> tester
= new JaxbTester<Your-Type>();
//test it
tester.test();
//what it does
Prints out sysout the XML output
Reads the XML output backup
Prints the XML out again to sysout
you can then verify
//An idea anyway!!
satya - 6/27/2018, 9:36:04 AM
An example
@XmlRootElement(name="PriceElement")
public class USPrice {
@XmlElement
public java.math.BigDecimal price;
}
satya - 6/27/2018, 9:49:05 AM
You can do this for a simple java class
@XmlRootElement(name="Employee")
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
public class AnySampleJavaClass
{
public String name;
public int age;
}
<Employee>
<name>hello</name>
<age>10</age>
</Employee>