I have an input xml and corresponding java classes as the following.
<Address>
<name>name</name>
<streetName>street</streetName>
<Address>
public class Address {
private String name;
//getter and setter here
}
public class Home extends Address {
private String streetName;
// getter and setter follows
}
And I tried to unmarshal the xml like the following
import com.thoughtworks.xstream.XStream;
public class Main {
public static void main(String as[]) {
XStream xstream = new XStream();
String str = "<com.xstream.inheritance.Address> <name>name</name> <streetName>street</streetName>"
+ "</com.xstream.inheritance.Address>";
Address address1 = (Address) xstream.fromXML(str);
System.out.println(address1.getName());
}
}
I got an exception saying Exception in thread "main"
com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$UnknownFieldException: No such field com.xstream.inheritance.Address.streetName
---- Debugging information ----
field : streetName
class : com.xstream.inheritance.Address
required-type : com.xstream.inheritance.Address
converter-type : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path : /com.xstream.inheritance.Address/streetName
version : null
Why did I get this exception?
The XML is missing the "class" attribute which would point to your extended class. So the output would be like this:
<com.xstream.inheritance.Address class="com.xstream.inheritance.Home">
<name>name</name>
<streetName>street</streetName>
</com.xstream.inheritance.Address>
This is done automatically by XStream.