任何人都可以告訴我爲什麼我的反序列化器不工作?簡單框架XML反序列化ValueRequiredException
Main.java
try {
Serializer serializer = new Persister();
AssetManager assetManager = getAssets();
InputStream inputStream = assetManager.open("data.xml");
Data d = serializer.read(Data.class, inputStream);
System.out.println("[JOE]: "+d.getPokemon().getName());
}
catch (Exception e) {
e.printStackTrace();
System.out.println("[JOE] error: "+e.getMessage());
}
data.xml中:
<Data>
<Pkmn>
<nm>Beedrill</nm>
<tp>bug</tp>
<ablt>swarm</ablt>
<wkns>fire</wkns>
<img>beedrill</img>
</Pkmn>
</Data>
Pokemon.java:
package com.example.pokemon;
import java.io.Serializable;
import org.simpleframework.xml.Element;
//import org.simpleframework.xml.Element;
public class Pokemon implements Serializable{
@Element
private String name;
@Element
private String type;
@Element
private String abilities;
@Element
private String weakness;
@Element
private String image;
public Pokemon(){}
public Pokemon(String n, String t, String a, String w, String i){
name = n;
type = t;
abilities = a;
weakness = w;
image = i;
}
public String getName(){
return name;
}
}
Data.java:
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
@Root(name="Data", strict=false)
public class Data {
@Element
private Pokemon pokemon;
public Pokemon getPokemon() {
return pokemon;
}
}
堆棧跟蹤:
謝謝先生。鍛鍊了魅力。 –