Solved

Create a custom binder for Jaxb

Posted on 2009-04-07
2
828 Views
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 ?

Thanks
@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

0
Comment
Question by:serrutom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 12

Accepted Solution

by:
Gibu George earned 500 total points
ID: 24087902
0
 
LVL 3

Author Comment

by:serrutom
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;
 
@XmlTransient
public class MyDateXmlAdapter extends XmlAdapter<String, MyDate> {
	private DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
 
	@Override
	public MyDate unmarshal(String s) throws Exception {
		if (s != null && s.length() > 0) {
			MyDate dt = new MyDate();
			try {
				dt.setTime(dateFormat.parse(s).getTime());
			} catch (Exception e) {
			}
			return dt;
		} else {
			return null;
		}
	}
 
	@Override
	public String marshal(MyDate date) throws Exception {
		if (date != null) {
			return dateFormat.format(date);
		} else {
			return null;
		}
	}
}

Open in new window

0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

733 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