Create a custom binder for Jaxb

Posted on 2009-04-07
Last Modified: 2013-12-29
I have an XML file I want to parse into an object with Jaxb. I have created the objects according to the XML file I receive, and annotated the getters to setup the binding.

The purchase date is coming in as a yyyyMMdd format, and this is not parsed correctly. I searched around a bit, and it seems I need to create a custom Jaxb binder.

Can anyone give me a jump-start on how i need to create this custom binder ?

@XmlType(name = "PRODUCTSHORT")

public class ProductShort {

	private String modelCode;

	private String serialNo;

	private MyDate purchaseDate;


	@XmlElement(name = "MODEL_CODE")

	public String getModelCode() {

		return modelCode;



	public void setModelCode(String modelCode) {

		this.modelCode = modelCode;



	@XmlElement(name = "SERIAL_NO")

	public String getSerialNo() {

		return serialNo;



	public void setSerialNo(String serialNo) {

		this.serialNo = serialNo;



	@XmlElement(name = "PURCHASE_DATE")

	public MyDate getPurchaseDate() {

		return purchaseDate;



	public void setPurchaseDate(MyDate purchaseDate) {

		this.purchaseDate = purchaseDate;



Open in new window

Question by:serrutom
LVL 12

Accepted Solution

Gibu George earned 500 total points
ID: 24087902

Author Comment

ID: 24088634
Works perfectly !!!

I created a MyDateXmlAdapter to convert the incoming date (String) to the MyDate class. And on the MyDate class I added following annotation: @XmlJavaTypeAdapter(MyDateXmlAdapter.class)
import java.text.DateFormat;

import java.text.SimpleDateFormat;

import javax.xml.bind.annotation.XmlTransient;

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


public class MyDateXmlAdapter extends XmlAdapter<String, MyDate> {

	private DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");


	public MyDate unmarshal(String s) throws Exception {

		if (s != null && s.length() > 0) {

			MyDate dt = new MyDate();

			try {


			} catch (Exception e) {


			return dt;

		} else {

			return null;




	public String marshal(MyDate date) throws Exception {

		if (date != null) {

			return dateFormat.format(date);

		} else {

			return null;




Open in new window


Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
matchUp  challenge 9 71
countAbc challenge 9 49
maven java path setting 5 50
eclipse compiler vs Installed JREs option 3 38
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now