使用 XStream 1.4.4。
我有以下 XML:
<app name="MyApp">
<properties>
<property name="fizz" value="buzz" />
<property name="foo" value="bar" />
</properties>
<fruit type="apple" />
<!-- ...etc. -->
</app>
以及属性列表的相应 POJO,以及属性本身:
@XStreamAlias("properties")
public class Properties {
private List<Property> properties = new ArrayList<Property>();
public List<Property> getProperties() {
return properties;
}
public void setProperties(List<Property> properties) {
this.properties = properties;
}
}
@XStreamAlias("property")
public class Property {
private String name = null;
private String value = null;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
当我尝试运行以下代码时:
XStream xs = new XStream();
Strnig xml = getXML(); // Fetches the string of XML from above
App app = (App)xs.fromXML(xml);
我得到:
Exception in thread "main" com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$UnknownFieldException: No such field com.me.myapp.Properties.property
---- Debugging information ----
field : property
class : com.me.myapp.Properties
required-type : com.me.myapp.Properties
converter-type : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path : /app/properties/property
line number : 4
class[1] : com.me.myapp.App
version : null
-------------------------------
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.determineType(AbstractReflectionConverter.java:453)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:294)
...rest of stack trace omitted for brevity
我哪里错了?