Using JAXB

From HiWiki

Jump to: navigation, search

PING Discovery: Pilot implementasjon og utforskning av PING -> Implementation -> Using JAXB

This section will explain the usage of JAXB in PING.

Contents

What is JAXB?

JAXB stands for Java Architecture for XML Binding, and is a framework for parsing between XML and Java.

JAXB in Maven

Ping uses Maven to build its project. In this it also generates Java classes from XML-schema files (.xsd). To be able to do this a JAXB plug-in for Maven is needed, and can be found here.

JAXB and Ping

When the classes have been created and the Servlet is running, the plug-in is no longer used. Instead the Servlet uses the standard JAXB libraries which can be found here. These libraries are downloaded automatically when the Ping project is compiled/installed. This is possible due to a nice feature in maven that allows you to specify repositories and dependencies in the project POM files.

With the standard JAXB 2.0 version we got errors when we tried to use our own marshal/unmarshal context, so instead we edited some of the POM files and downloaded version 2.0.2 (we found each POM file which had a jaxb-impl dependency and changed the version of jaxb-impl from 2.0 to 2.0.2). There was no error when we used the standard Ping marshal/unmarshal context with JAXB 2.0.

Marshalling

Marshalling (API) is the process of going from a Java object to XML. In PING this is used in three different settings:

  1. in the document body
  2. in action wrapper
  3. communication

The first one is used when wrapping document content into an element in the document body, and is done like this:

//marshalling an Epikrise:
JAXBContext epikriseContext = null;
try {
    epikriseContext = JAXBContext.newInstance("no.kith.xmlstds.epikrise");
} catch (JAXBException jaxbe) {
    System.out.println("Feil med context!");
    jaxbe.printStackTrace();
    //shouldn't happen
}
// The anyElement in the document body:
Element anyElement = JAXBUtils.marshalToElement(new Message(msg), epikriseContext);

The last two are lightly explained in How the client handles requests and responses, but works in a similar way.

Unmarshalling

Unmarshalling (API) is the process of going from XML to Java objects. This is used in corresponding situations as the marshalling.

When wrapping up the document content from an element in the document body:

// unmarshalling an Epikrise:
MessageType message = null;
try{
    JAXBContext epikriseContext = JAXBContext.newInstance("no.kith.xmlstds.epikrise");
    Unmarshaller unmarshaller = epikriseContext.createUnmarshaller();
    JAXBElement tmp = (JAXBElement) unmarshaller.unmarshal(element);
    message = (MessageType) tmp.getValue();
} catch(Exception e)

It is also possible to unmarshal XML-files without problems:

JAXBElement element = null;
try{
    JAXBContext context = JAXBContext.newInstance("no.kith.xmlstds.epikrise");
    Unmarshaller u = context.createUnmarshaller();
    element = (JAXBElement) u.unmarshal(new File("Epikrise-ex-2.xml"));
} catch(Exception e){
    System.out.println("Feil ved unmarshalling av fil.");
    e.printStackTrace();
}