4

编辑:删除以前的编辑

我正在尝试反序列化以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<ALL>
    <KAMP>
        <ID>1</ID>
        <SQLTID>1376881200</SQLTID>
        <DATO>2013-08-19</DATO>
    </KAMP>
    ...
    <KAMP>
        <ID>2</ID>
        <SQLTID>1376881200</SQLTID>
        <DATO>2013-08-19</DATO>
    </KAMP>
</ALL>

使用

@Root
public class Matches {

    @ElementList
    private List<Match> list;

    public List getMatches() {
        return list;
    }
}

@Root(name = "KAMP", strict = false)
public class Match{

    @Element(name = "ID", required = false)
    public String Id;

    @Element(name = "SQLTID", required = false)
    public String Sqltid;

    @Element(name = "DATO", required = false)
    public String MatchDate;
}

我不断得到

Element 'KAMP' does not have a match in class <myClass>

我试过添加(name = "KAMP")@ElementList但没有帮助。

其他人可以帮忙吗?

4

1 回答 1

7

以下代码适用于我,请特别注意以下两项:

  1. ALL.java 中的空构造函数
  2. 该列表具有注释inline=true

ALL.java:

import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import java.io.File;
import java.util.List;

/**
 * User: alfasin
 * Date: 8/19/13
 */
@Root(name="ALL")
public class ALL {

    @ElementList(entry="KAMP", inline=true)
    private List<KAMP> kamp;

    public ALL(){};

    public List<KAMP>  getMatches() {
        return kamp;
    }

    public static void main(String...args) throws Exception {
        Serializer serializer = new Persister();
        File example = new File("/Users/alfasin/Documents/workspace-sts-3.2.0.RELEASE/SimpleXML/src/kamp.xml");
        ALL all = serializer.read(ALL.class, example);
        for(KAMP tmp : all.getMatches()){
            System.out.println("ID: "+tmp.Id);
            System.out.println("MatchDate: "+tmp.MatchDate);
            System.out.println("Sqltid: "+tmp.Sqltid);
            System.out.println("----------");
        }
    }
}

KAMP.java

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

/**
 * User: alfasin
 * Date: 8/19/13
 */
@Root(name="KAMP",strict = false)
public class KAMP {

    @Element(name = "ID", required = false)
    public String Id;

    @Element(name = "SQLTID", required = false)
    public String Sqltid;

    @Element(name = "DATO", required = false)
    public String MatchDate;

}

输出

ID: 1
MatchDate: 2013-08-19
Sqltid: 1376881200
----------
ID: 2
MatchDate: 2013-08-19
Sqltid: 1376881200
----------
于 2013-08-19T15:22:56.050 回答