编辑:删除了之前的修改
我试图反序列化以下内容:
<?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
,但它没有帮助。
其他人可以帮忙吗?
答案 0 :(得分:7)
以下代码适用于我,请特别注意以下两项:
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
----------