问题
This is the frist time I am trying XStream. But when I try to parse my xml file i am getting this exception :
com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$UnknownFieldException: No such field xmlread.Type.type
---- Debugging information ----
field : type
class : xmlread.Type
required-type : xmlread.Type
converter-type : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path : /root/type
line number : 10
version : 1.4.8
-------------------------------
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.handleUnknownField(AbstractReflectionConverter.java:501)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:357)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:263)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134)
at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1206)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1190)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1061)
at xmlread.Main.main(Main.java:23)
So we can see that the error is on row 10 in my Type class. And row 10 is the this line : @XStreamAlias("root")
package xmlread;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
@XStreamAlias("root")
public class Type {
@XStreamAlias("INFO")
private Info info;
@XStreamAlias("OBJECT_TYPE")
private String objectType;
@XStreamAlias("PROP")
private Properties prop;
@XStreamAlias("PARAM")
private List<Parameters> param;
@XStreamImplicit(itemFieldName = "type")
private List typeList = new ArrayList();
// metodo construtor
public Type() {
// TODO Auto-generated constructor stub
}
//Constructor, Getters and setters
public Info getInfo() {
return info;
}
public void setInfo(Info info) {
this.info = info;
}
public String getObjectType() {
return objectType;
}
public void setObjectType(String objectType) {
this.objectType = objectType;
}
public Properties getProp() {
return prop;
}
public void setProp(Properties prop) {
this.prop = prop;
}
public List<Parameters> getParam() {
return param;
}
public void setParam(List<Parameters> param) {
this.param = param;
}
public List getTypeList() {
return typeList;
}
public void setTypeList(List typeList) {
this.typeList = typeList;
}
}
And the xml looks like this :
<root>
<info>
<CORE_NAME>DataModel_Core</CORE_NAME>
<CORE_VERSION>..</CORE_VERSION>
<CORE_PRODUCT_ID>...</CORE_PRODUCT_ID>
<ADAPTATION_NAME />
<ADAPTATION_VERSION />
<ADAPTATION_PRODUCT_ID />
</info>
<type>
<OBJECT_TYPE>data</OBJECT_TYPE>
<prop>
<DESCRIPTION>Site parameters</DESCRIPTION>
<PARENT>NULL</PARENT>
<VIRTUAL>0</VIRTUAL>
<VISIBLE>1</VISIBLE>
<PICTURE>NULL</PICTURE>
<HELP>10008</HELP>
<MIN_NO>1</MIN_NO>
<MAX_NO>1</MAX_NO>
<NAME_FORMAT>NULL</NAME_FORMAT>
</prop>
<param>
<PARAMETER>nidRbc</PARAMETER>
<DATA_TYPE>INTEGER</DATA_TYPE>
<DESCRIPTION>RBC identity</DESCRIPTION>
<MIN_NO>1</MIN_NO>
<MAX_NO>1</MAX_NO>
<ORDER1>1</ORDER1>
<NESTED>0</NESTED>
<DEFAULT1>NULL</DEFAULT1>
<FORMAT>0:16382</FORMAT>
</param>
</type>
</root>
And my other classes looks like this :
package xmlread;
import java.io.*;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.XStreamException;
public class Main {
public static void main(String[] args) throws Exception {
try {
FileReader reader = new FileReader("enderecos.xml");
//String representacao = xstream.toXML(aluno);
XStream xstream = new XStream();
xstream.processAnnotations(Properties.class);
xstream.processAnnotations(Parameters.class);
xstream.processAnnotations(ObjType.class);
xstream.processAnnotations(Type.class);
Type data = (Type)xstream.fromXML(reader);
//Person newJoe = (Person)xstream.fromXML(xml);
System.out.println(data);
} catch (IOException io) {
io.printStackTrace();
} catch (XStreamException e) {
e.printStackTrace();
}
}
}
____________________________________________________________
package xmlread;
import java.util.List;
import com.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias("PARAM")
public class Parameters {
@XStreamAlias("DATATYPE")
private String datatype;
@XStreamAlias("DESCRIPTION")
private String description;
@XStreamAlias("MIN_NO")
private String min_no;
@XStreamAlias("MAX_NO")
private String max_no;
@XStreamAlias("ORDER1")
private String order1;
@XStreamAlias("NESTED")
private String nested;
@XStreamAlias("DEFAULT1")
private String default1;
@XStreamAlias("FORMAT")
private String format;
@XStreamAlias("PARAMETER")
private List<Parameters> parameter;
public Parameters(String datatype, String description, String min_no,
String max_no, String order1, String nested, String default1,
String format, List<Parameters> parameter) {
super();
setDatatype(datatype);
setDescription(description);
setMin_no(min_no);
setMax_no(max_no);
setOrder1(order1);
setNested(nested);
setDefault1(default1);
setFormat(format);
setParameter(parameter);
} // Getters and setters..
public Parameters() {
// TODO Auto-generated constructor stub
}
public String getDatatype() {
return datatype;
}
public void setDatatype(String datatype) {
this.datatype = datatype;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getMin_no() {
return min_no;
}
public void setMin_no(String min_no) {
this.min_no = min_no;
}
public String getMax_no() {
return max_no;
}
public void setMax_no(String max_no) {
this.max_no = max_no;
}
public String getOrder1() {
return order1;
}
public void setOrder1(String order1) {
this.order1 = order1;
}
public String getNested() {
return nested;
}
public void setNested(String nested) {
this.nested = nested;
}
public String getDefault1() {
return default1;
}
public void setDefault1(String default1) {
this.default1 = default1;
}
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
public List<Parameters> getParameter() {
return parameter;
}
public void setParameter(List<Parameters> parameter) {
this.parameter = parameter;
}
}
____________________________________________________________
package xmlread;
import com.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias("prop")
public class Properties {
@XStreamAlias("DESCRIPTION")
private String description;
@XStreamAlias("PARENT")
private String parent;
@XStreamAlias("VIRTUAL")
private String virtual;
@XStreamAlias("VISIBLE")
private String visible;
@XStreamAlias("PICTURE")
private String picture;
@XStreamAlias("HELP")
private String help;
@XStreamAlias("MIN_NO")
private String min_no;
@XStreamAlias("MAX_NO")
private String max_no;
@XStreamAlias("NAME_FORMAT")
private String name_format;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getParent() {
return parent;
}
public void setParent(String parent) {
this.parent = parent;
}
public String getVirtual() {
return virtual;
}
public void setVirtual(String virtual) {
this.virtual = virtual;
}
public String getVisible() {
return visible;
}
public void setVisible(String visible) {
this.visible = visible;
}
public String getPicture() {
return picture;
}
public void setPicture(String picture) {
this.picture = picture;
}
public String getHelp() {
return help;
}
public void setHelp(String help) {
this.help = help;
}
public String getMin_no() {
return min_no;
}
public void setMin_no(String min_no) {
this.min_no = min_no;
}
public String getMax_no() {
return max_no;
}
public void setMax_no(String max_no) {
this.max_no = max_no;
}
public String getName_format() {
return name_format;
}
public void setName_format(String name_format) {
this.name_format = name_format;
}
public static void setInstance(Properties instance) {
Properties.instance = instance;
}
public static Properties instance = null;
public static Properties getInstance() {
if (instance == null) {
instance = new Properties();
}
return instance;
}
public Properties() {
// TODO Auto-generated constructor stub
}
}
____________________________________________________________
package xmlread;
import com.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias("INFO")
public class Info {
@XStreamAlias("CORE_NAME")
private String core_name;
@XStreamAlias("CORE_VERSION")
private String core_version;
@XStreamAlias("CORE_PRODUCT_ID")
private String core_product_id;
@XStreamAlias("ADAPTATION_NAME")
private String adaptation_name;
@XStreamAlias("ADAPTATION_VERSION")
private String adaptation_version;
@XStreamAlias("ADAPTATION_PRODUCT_ID")
private String adaptation_product_id;
public Info(String core_name, String core_version, String core_product_id,
String adaptation_name, String adaptation_version,
String adaptation_product_id) {
super();
setCore_name(core_name);
setCore_version(core_version);
setCore_product_id(core_product_id);
setAdaptation_name(adaptation_name);
setAdaptation_version(adaptation_version);
setAdaptation_product_id(adaptation_product_id);
}
public String getCore_name() {
return core_name;
}
public void setCore_name(String core_name) {
this.core_name = core_name;
}
public String getCore_version() {
return core_version;
}
public void setCore_version(String core_version) {
this.core_version = core_version;
}
public String getCore_product_id() {
return core_product_id;
}
public void setCore_product_id(String core_product_id) {
this.core_product_id = core_product_id;
}
public String getAdaptation_name() {
return adaptation_name;
}
public void setAdaptation_name(String adaptation_name) {
this.adaptation_name = adaptation_name;
}
public String getAdaptation_version() {
return adaptation_version;
}
public void setAdaptation_version(String adaptation_version) {
this.adaptation_version = adaptation_version;
}
public String getAdaptation_product_id() {
return adaptation_product_id;
}
public void setAdaptation_product_id(String adaptation_product_id) {
this.adaptation_product_id = adaptation_product_id;
}
}
____________________________________________________________
package xmlread;
import com.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias("OBJECT_TYPE")
public class ObjType {
@XStreamAlias("OBJECT_TYPE")
private String objectType;
public ObjType() {
} // Getters and setters
public String getObjectType() {
return objectType;
}
public void setObjectType(String objectType) {
this.objectType = objectType;
}
}
Whats the cause of this error?
回答1:
package xmlread;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
@XStreamAlias("root")
public class SomeOtherClass {
@XStreamAlias("INFO")
private Info info;
@XStreamAlias("TYPE")
private Type type;
@XStreamImplicit(itemFieldName = "type")
private List typeList = new ArrayList();
// metodo construtor
public SomeOtherClass() {
// TODO Auto-generated constructor stub
}
//Constructor, Getters and setters
public void setType(Type type){
this.type = type;
}
public Type getType() {
return this.type;
}
public Info getInfo() {
return info;
}
public void setInfo(Info info) {
this.info = info;
}
}
class Type {
@XStreamAlias("OBJECT_TYPE")
private String objectType;
@XStreamAlias("PROP")
private Properties prop;
@XStreamAlias("PARAM")
private List<Parameters> param;
public String getObjectType() {
return objectType;
}
public void setObjectType(String objectType) {
this.objectType = objectType;
}
public Properties getProp() {
return prop;
}
public void setProp(Properties prop) {
this.prop = prop;
}
public List<Parameters> getParam() {
return param;
}
public void setParam(List<Parameters> param) {
this.param = param;
}
}
来源:https://stackoverflow.com/questions/29050537/com-thoughtworks-xstream-converters-reflection-abstractreflectionconverterunkno