gpt4 book ai didi

java - xStream 无法正确处理列表和属性

转载 作者:行者123 更新时间:2023-12-01 15:21:31 26 4
gpt4 key购买 nike

我正在尝试创建 XML 文件的一些对象。但我做错了,这是我放入 xml 文件的方法:

    XStream x = new XStream();
XML v = (XML) x.fromXML("<maxi-xml><name>World League</name><type>15</type><season>33</season><date>2012-05-27T18:00:00+02:00</date><arenaId>2191</arenaId><nationId>4</nationId><regionId>85</regionId><standings>2</standings><attendance>20000</attendance><meteo>3</meteo><status>3</status><event id=8228607/><event id=8228608/><event id=8228609/><event id=8228610/><event id=8228611/><event id=8228612/><event id=8228613/><event id=8228614/><event id=8228615/><event id=8228616/></maxi-xml>");
System.out.println("versie" + v.toString());

这是 xml 文件:

<maxi-xml>
<name>National League 2.1 Nederland</name>
<type>15</type>
<season>33</season>
<date>2012-05-27T18:00:00+02:00</date>
<arenaId>2191</arenaId>
<nationId>4</nationId>
<regionId>85</regionId>
<standings>2</standings>
<attendance>20000</attendance>
<meteo>3</meteo>
<status>3</status>
<event id="8228607"/>
<event id="8228608"/>
<event id="8228609"/>
<event id="8228610"/>
<event id="8228611"/>
<event id="8228612"/>

这是我正在使用的类:

比赛类别:

 @XStreamAlias("maxi-xml")
public class Competition {

String name;
int type;
int season;
Date date;
int arenaId;
int nationId;
int regionId;
int standings;
int attendance;
int meteo;
int status;
@XStreamImplicit
ArrayList<Event> event;

public Competition( String name, int type, int season, Date date, int arenaId, int nationId, int regionId, int standings,
int attendance, int meteo, int status, ArrayList<Event> event) {
this.name = name;
this.type = type;
this.season = season;
this.date = date;
this.arenaId = arenaId;
this.nationId = nationId;
this.regionId = regionId;
this.standings = standings;
this.attendance = attendance;
this.meteo = meteo;
this.status = status;
this.event = event;
}

}

事件类:

public class Event {

@XStreamAsAttribute
int id;

public Event(int id) {
this.id = id;
}

}

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

这就是我以相反的方式得到的结果:

使用这段代码:

    XStream x = new XStream();
ArrayList<Events> list = new ArrayList<Events>();
list.add(new Events(16));
list.add(new Events(1364));
list.add(new Events(1365));
list.add(new Events(1234));
list.add(new Events(5679));

XML xml = new XML("something", 1, 22, new Date(), 2, 2, 2, 2, 232434, 1, 3, list);
String s = x.toXML(xml);
System.out.println(s);



<Competition>
<name>something</name>
<type>1</type>
<season>22</season>
<date>2012-06-01 09:18:27.161 UTC</date>
<arenaId>2</arenaId>
<nationId>2</nationId>
<regionId>2</regionId>
<standings>2</standings>
<attendance>232434</attendance>
<meteo>1</meteo>
<status>3</status>
<event>
<Event>
<id>16</id>
</Event>
<Event>
<id>1364</id>
</Event>
<Event>
<id>1365</id>
</Event>
<Event>
<id>1234</id>
</Event>
<Event>
<id>5679</id>
</Event>
</event>
</Competition>

最佳答案

检查以下网址是否有帮助。

http://x-stream.github.io/alias-tutorial.html

关于java - xStream 无法正确处理列表和属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10847219/

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