Showing posts with label JAXB. Show all posts
Showing posts with label JAXB. Show all posts

Tuesday, July 29, 2014

JAXB marshalling and unmarshalling of Hashtable

It is not possible to Marshall or Unmarshall a Hashtable directly however you can achieve it using a Holder class (wrapper class). With the help of Holder class you can get the default XML format as shown below -

Default XML format -
<mapHolder>
    <htmlColorCode>
        <entry>
            <key>blue</key>
            <value>0000FF</value>
        </entry>
        <entry>
            <key>green</key>
            <value>00FF00</value>
        </entry>
        <entry>
            <key>red</key>
            <value>FF0000</value>
        </entry>
    </htmlColorCode>
</mapHolder>

However if you need to customize the XML format like the one shown below you may need to use XmlAdapter.

<colorMap>
    <colors>
        <item code="0000FF" color="blue"/>
        <item code="00FF00" color="green"/>
        <item code="FF0000" color="red"/>
    </colors>
</colorMap>

Method 1 - Using Wrapper class

Output -
--Marshalling--
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mapHolder>
    <htmlColorCode>
        <entry>
            <key>blue</key>
            <value>0000FF</value>
        </entry>
        <entry>
            <key>green</key>
            <value>00FF00</value>
        </entry>
        <entry>
            <key>red</key>
            <value>FF0000</value>
        </entry>
    </htmlColorCode>
</mapHolder>

--Unmarshalling--
{blue=0000FF, green=00FF00, red=FF0000}


Java - JAX-WS Jar files for SOAP web services

If you are writing a SOAP based webservice you may need to add below JAR files in your applications classpath

Error: java.lang.ClassNotFoundException: com.sun.xml.ws.transport.http.servlet.WSServletContextListener
Add: jaxws-rt.jar

Error: java.lang.NoClassDefFoundError: com/sun/istack/localization/Localizable
Add: jaxb-core.jar

Error: java.lang.NoClassDefFoundError: com/sun/xml/stream/buffer/XMLStreamBuffer
Add: streambuffer.jar

Error: java.lang.NoClassDefFoundError: com/sun/xml/bind/api/JAXBRIContext
Add: jaxb-impl.jar

Error: java.lang.NoClassDefFoundError: com/sun/xml/ws/policy/PolicyException
Add: policy.jar

Error: java.lang.NoClassDefFoundError: org/jvnet/staxex/XMLStreamReaderEx
Add: stax-ex.jar

Error: java.lang.NoClassDefFoundError: org/glassfish/gmbal/ManagedObjectManager
Add: gmbal-api-only.jar

Error: java.lang.NoClassDefFoundError: org/glassfish/external/amx/AMXGlassfish
Add: management-api.jar

Error: java.lang.NoClassDefFoundError: org/glassfish/ha/store/api/BackingStoreException
Add: ha-api.jar

You can download latest version of JAR file (E.g. jaxws-ri-2.2.8.zip ) from https://jax-ws.java.net/2.2.8/. For more information please see the link https://jax-ws.java.net/.

Java - JAXB marshalling and unmarshalling of ArrayList

Marshalling and unmarshalling of ArrayList using JAXB is rather simple however you cannot directly marshall ArrayList. You will need a holder class (wrapper class). It will hold the arraylist that you want to marshall or unmarshall.

Please see the self explanatory java code below -

Output -

--Marshalling--
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<List>
    <Value>one</Value>
    <Value>two</Value>
    <Value>three</Value>
</List>

--Unmarshalling--

[one, two, three]

Friday, April 6, 2012

Java : Simple JAXB marshalling and unmarshalling Hello World Example



JAXB is an acronym derived from Java Architecture for XML Binding. This articles shows how to convert Java object to XML using JAXB annotation and vice versa. If you would like to know more about JAXB, see below links.


Marshalling - Convesrting a Java object to XML is known as Marshalling.
Unmarshalling - Converting a XML to Java Object is known as Unmarshalling.

About the example :

We have a Employee class which contains Name class reference in it. i.e HAS-A relation. Along with emplId and dept. TestMarshalling.java contains marshalIt() and unmarshalIt() method. marshalIt() as the name suggest, it accepts Employee object and return XML string out of it. And similarly unmarshalIt() method takes Class name and XML string and return Employee object out of it.

marshalIt(Object objectName) method uses JAXBContext to create Marshaller object

JAXBContext jaxbContext = JAXBContext.newInstance(objectName.getClass());
Marshaller marshaller = jaxbContext.createMarshaller();

Similarly unmarshalIt(Class className,String xml) method use JAXBContext to create Unmarshaller object

JAXBContext jaxbContext = JAXBContext.newInstance(className);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

No need to include any JAR file if you are using Java 1.6 and above because JAXB is bundled in JDK 1.6. Directly you can run below example and see the output. However if you are using Java 1.5 or lower version you need to include JAR file (e.g JAXB2_20120218.jar ) from http://jaxb.java.net/

Below are the self explanatory Java example. Directly you can run the example and see the output no need to have any IDE (e.g. Eclipse)


Name.java

/* Name.java */

public class Name {

 private
String firstName;
 private
String lastName;

 public String getFirstName() {
  return firstName;
 }


 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }


 public String getLastName() {
  return lastName;
 }


 public void setLastName(String lastName) {
  this.lastName = lastName;
 }
}