【发布时间】:2013-06-11 16:35:42
【问题描述】:
我有一个像这样的简单 xml 字符串
<table>
<test_id>t59</test_id>
<dateprix>2013-06-06 21:51:42.252</dateprix>
<nomtest>NOMTEST</nomtest>
<prixtest>12.70</prixtest>
<webposted>N</webposted>
<posteddate>2013-06-06 21:51:42.252</posteddate>
</table>
我有这样的 xml 字符串的 pojo 类
@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;
}
我正在使用 jaxb 将 xml 绑定到 java 对象。代码是
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();
}
现在我的问题是,在与 java 对象绑定后,我的日期变量(dateprix 和posteddata)为空,所以我怎么能得到这个值。
如果我使用“2013-06-06”,我得到了数据对象,但对于“2013-06-06 21:51:42.252”,我得到了 null。
【问题讨论】:
-
检查 stackoverflow.com/questions/5775860/… 以获取正确的日期格式。