gpt4 book ai didi

java - xml 日期和时间字符串上的 XStream 转换异常

转载 作者:行者123 更新时间:2023-12-01 05:38:24 25 4
gpt4 key购买 nike

我尝试转换的 XML 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfTrafiklage>
<Trafiklage>
<Utgangsdatum xmlns="">2011-10-14T00:00:00+02:00</Utgangsdatum>
</Trafiklage>
<Trafiklage>
<Utgangsdatum xmlns="">2011-10-15T00:00:00+02:00</Utgangsdatum>
</Trafiklage>
<Trafiklage>
<Utgangsdatum xmlns="">2011-10-16T00:00:00+02:00</Utgangsdatum>
</Trafiklage>
</ArrayOfTrafiklage>

我要转换的代码如下所示:

XStream xstream = new XStream(new DomDriver());

xstream.alias("ArrayOfTrafiklage", Trafiklagen.class);
xstream.alias("Trafiklage", StationInfoModel.class);

Trafiklagen trafiklagen = (Trafiklagen) xstream
.fromXML(result);

StationInfoModel.java 如下所示:

public class StationInfoModel 
{
private Date Utgangsdatum;
}

当它运行 fromXML 命令时,我收到此异常:

com.thoughtworks.xstream.converters.ConversionException: Utgangsdatum : Utgangsdatum
---- Debugging information ----
message : Utgangsdatum
cause-exception : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message : Utgangsdatum
class : java.util.ArrayList
required-type : java.util.ArrayList
converter-type : com.thoughtworks.xstream.converters.collections.CollectionConverter
path : /ArrayOfTrafiklage/Trafiklage/Utgangsdatum
class[1] : org.rickardp.trainstatus.Models.Trafiklagen
converter-type[1] : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
version : 0.0
-------------------------------

有人知道我做错了什么吗?

最佳答案

使用这个

private static SimpleDateFormat frmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S z");
frmt.format(date)

关于java - xml 日期和时间字符串上的 XStream 转换异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7771260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com