Einn Hann
Einn Hann

Reputation: 711

Can JAXB unmarshal a string into a date attribute

I have a xml tag in this format:

<DOB>19801213</DOB>

How can I unmarshal this xml tag into a Date variable?

@XmlElement (name = "DOB")
private Date dob

When I try to get dob it return me null.

Upvotes: 2

Views: 660

Answers (1)

Xstian
Xstian

Reputation: 8272

You must use an XmlAdapter

import java.util.Date;

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

public class DateAdapter extends XmlAdapter<String, Date> {

    @Override
    public String marshal(Date v) throws Exception {
        return .. ;
    }

    @Override
    public Date unmarshal(String v) throws Exception {
        return .. ;
    }

}

and on your attribute you must add @XmlJavaTypeAdapter

@XmlJavaTypeAdapter(DateAdapter.class)
@XmlElement (name = "DOB")
private Date dob;

Upvotes: 2

Related Questions