Solved

Create a custom binder for Jaxb

Posted on 2009-04-07
2
826 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
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet  URL Rewriting 1 38
Maven Project: Hibernate Dependencies Conflict 10 26
eclipse buid path vs tomcat lib path 10 24
Java: anonymous class 4 23
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

825 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