tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 XML > JAXB > Xml Adapter

Xml Adapter 

The following example shows customizing XML serialization/deserialization process using XmlAdapter. We can specify the adapter to use by annotating the required property with @XmlJavaTypeAdapter annotation.

File Name  :  
com/bethecoder/tutorials/jaxb/common/adapter/SecretNumberXmlAdapter.java 
   
package com.bethecoder.tutorials.jaxb.common.adapter;

import javax.xml.bind.annotation.adapters.XmlAdapter;

import com.bethecoder.tutorials.jaxb.common.SecretNumber;

public class SecretNumberXmlAdapter extends XmlAdapter<String, SecretNumber> {

  @Override
  public String marshal(SecretNumber paramBoundTypethrows Exception {
    return Integer.toBinaryString(paramBoundType.getNumber());
  }

  @Override
  public SecretNumber unmarshal(String paramValueTypethrows Exception {
    return new SecretNumber(Integer.parseInt(paramValueType, 2));
  }

}
   

File Name  :  
com/bethecoder/tutorials/jaxb/common/Foo.java 
   
package com.bethecoder.tutorials.jaxb.common;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import com.bethecoder.tutorials.jaxb.common.adapter.SecretNumberXmlAdapter;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD//Allows to get data through fields rather than public getter/setters
public class Foo {
  
  @XmlJavaTypeAdapter(SecretNumberXmlAdapter.class)
  private SecretNumber secretNumber;

  public SecretNumber getSecretNumber() {
    return secretNumber;
  }

  public void setSecretNumber(SecretNumber secretNumber) {
    this.secretNumber = secretNumber;
  }
}
   

File Name  :  
com/bethecoder/tutorials/jaxb/common/SecretNumber.java 
   
package com.bethecoder.tutorials.jaxb.common;

public class SecretNumber {
  private int number;

  public SecretNumber() {
  }

  public SecretNumber(int number) {
    super();
    this.number = number;
  }

  public int getNumber() {
    return number;
  }

  public void setNumber(int number) {
    this.number = number;
  }
}
   

File Name  :  
com/bethecoder/tutorials/jaxb/tests/XmlAdapterTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.jaxb.tests;

import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import com.bethecoder.tutorials.jaxb.common.Foo;
import com.bethecoder.tutorials.jaxb.common.SecretNumber;

public class XmlAdapterTest {

  public static void main (String [] argsthrows JAXBException {
    
    Foo foo = new Foo();
    foo.setSecretNumber(new SecretNumber(1729));
    
    /**
     * Create JAXB Context from the classes to be serialized
     */
    StringWriter output = new StringWriter();
    JAXBContext context = JAXBContext.newInstance(Foo.class);  
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);  
    m.marshal(foo, output);  
    System.out.println(output.toString());
    
    /**
     * Unmarshall
     * Get the Foo object from XML string
     */
    Unmarshaller um = context.createUnmarshaller();
    StringReader sr = new StringReader(output.toString());
    foo = (Fooum.unmarshal(sr);
    System.out.println("Secret Number : " + foo.getSecretNumber().getNumber());
  }
}
   

It gives the following output,
File Name  :  OUTPUT



 
  


  
bl  br