Working with Jaxb in 2017


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

JAXBContext Java API

Search for: JAXBContext Java API

Jaxb annotations

Search for: Jaxb annotations

Jaxb Marshaller API documentation

Search for: Jaxb Marshaller API documentation

javax.xml.bind API

Search for: javax.xml.bind API

All of javax.xml.bind classes

Marshaller.JAXB_FORMATED_OUTPUT

Search for: Marshaller.JAXB_FORMATED_OUTPUT

You can find the supported properties here

javax.xml.bind.annotation API

Search for: javax.xml.bind.annotation API

Here are all the annotations

What are key jaxb annotations

Search for: What are key jaxb annotations

jdk 1.8 API

Search for: jdk 1.8 API


@XmlRootElement
@XmlRootElement(name="myroot")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder={"field1","field5","field4"})
@XmlElement(name="myelem")
@XmlJavaTypeAdapter(MapAdapter.class)
@XmlAnyElement

XmlAccessType.FIELD

Search for: XmlAccessType.FIELD

Here are docs on this field

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.

XmlAnyElement in Jaxb

Search for: XmlAnyElement in Jaxb

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.

Looks like one needs a whole guide to unravel all the possibilities!!!

A couple of code samples

jaxb converting java Map object to XML annotations?

Search for: jaxb converting java Map object to XML annotations?

JAXB and java.util.Map: a good article and sample code

XmlAdapter - JAXB's Secret Weapon: another article, 2010

@XmlJavaTypeAdapter

Search for: @XmlJavaTypeAdapter

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.

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.

What is XmlStreamWriter in Java?

Search for: What is XmlStreamWriter in Java?

Here is a good article on XmlStreamWriter

When you want to explicitly write XML files element by element, comment by comment, name space by name space etc.

Universal Jaxb tester

Search for: Universal Jaxb tester


//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!!

Docs on root element


@XmlRootElement(name="PriceElement")
public class USPrice {
     @XmlElement
     public java.math.BigDecimal price;
}

@XmlRootElement(name="Employee")
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
public class AnySampleJavaClass
{
   public String name;
   public int age;
}

<Employee>
  <name>hello</name>
  <age>10</age>
</Employee>