XML date Bind to Java object dates

See answer in English> jaxb unmarshal timestamp 4
I have a simple xml string like this


t59
2013-06-06 21:51:42.252
NOMTEST< br /> 12.70
N
2013-06-06 21:51:42.252

I have a pojo class for xml strings like this

@XmlRootElement(name="test")
public class Test {
@XmlElement
public String test_id;
@XmlElement
public Date dateprix;
@XmlElement
public String nomtest;
@XmlElement< br /> public double prixtest;
@XmlElement
public char webposted;
@XmlElement
public Date posteddate;
}

I use jaxb for xml is bound to java object. The code is

try {
Test t = new Test
JAXBContext jaxbContext = JAXBContext.newInstance(t.getC lass());
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
t = (Test) jaxbUnmarshaller.unmarshal(new InputSource(new StringReader(xml))); // xml variable contain the xml string define above
} catch (JAXBException e) {
e.printStackTrace();
}

Now my problem is that after binding with the java object, I I get null for the date variables (dateprix and posteddata), then how can I get this value.

If I use “2013-06-06” I get the data object but for “2013-06-06 21:51:42.252” I got null.

JAXB expects the date in the XML to be xsd: date(yyyy-MM-dd) or xsd: dateTime format (yyyy-MM-ddTHH: mm: ss.sss). 2013-06-06 21:51:42.252 is not a valid dateTime format’T’ (date/time separated Character) is missing. You need a custom XmlAdapter to enable JAXB to convert it to Java Date. For example,

class DateAdapter extends XmlAdapter {
DateFormat f = new SimpleDateFormat("yyy-MM-dd HH:mm:ss.SSS");

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

@Override
publi c String marshal(Date v) throws Exception {
return f.format(v);
}
}

class Type {
@XmlJavaTypeAdapter( DateAdapter.class)
public Date dateprix;
...

See the answer in English> jaxb unmarshal timestamp 4 jaxb Such a simple xml string


t59
2013-06- 06 21:51:42.252
NOMTEST
12.70
N
2013-06-06 21:51:42.252

I have a pojo class for xml strings like this

@XmlRootElement(name="test")
public class Test {
@XmlElement
public String test_id;
@XmlElement
public Date dateprix;
@XmlElement
public String nomtest;
@XmlElement
public double prixtest;
@XmlElement
public char webposted;
@XmlElement
public Date posteddate;
}

I use jaxb for xml to bind to java objects. The code is

try {
Test t = new Test
JAXBContext jaxbContext = JAXBContext.newInstance(t.getClass());
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
t = (Test) jaxbUnmarshaller.unmarshal(new InputSource (new StringReader(xml))); // xml variable contain the xml string define above
} catch (JAXBException e) {
e.printStackTrace();
}

< p>Now my question is, after binding with the java object, I get null for the date variables (dateprix and posteddata), then how can I get this value.

If I use “2013- 06-06” I got the data object but for “2013-06-06 21:51:42.252” I got null.

JAXB expects XML The date is xsd: date (yyyy-MM-dd) or xsd: dateTime format (yyyy-MM-ddTHH: mm: ss.sss). 2013-06-06 21:51:42.252 is not a valid dateTime format’T’ (Date/time separator) is missing. You need a custom XmlAdapter to make JAXB convert it to Java Date. For example,

class DateAdapter extends XmlAdapter {
DateFormat f = new SimpleDateFormat("yyy-MM-dd HH:mm:ss.SSS");

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

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

class Type {
@XmlJavaTypeAdapter(DateAdapter.class)
public Date dateprix;
...

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 6024 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.