使用列表和属性,xStream无法正常工作

时间:2012-06-01 08:52:37

标签: java xml xstream

我试图制作一些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>

1 个答案:

答案 0 :(得分:1)

如果有帮助,请查看以下网址。

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