Using XStream 1.4.4.
I have the following XML:
<app name="MyApp">
<properties>
<property name="fizz" value="buzz" />
<property name="foo" value="bar" />
</properties>
<fruit type="apple" />
<!-- ...etc. -->
</app>
And respective POJOs for the list of properties, as well as the properties themselves:
@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;
}
}
When I try running the following code:
XStream xs = new XStream();
Strnig xml = getXML(); // Fetches the string of XML from above
App app = (App)xs.fromXML(xml);
I get:
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
Where am I going wrong?
You don't need annotations at all. Just use alias Property.class
as "properties" and XStream will take care of everything else for you.