Search code examples
javaxmlxml-parsingxstream

xstream is not parsing root element


I am using 'com.thoughtworks.xstream:xstream:1.4.10' library and trying to parse xml files.

Broker is the root element and there are other tags inside

<broker>
   <othertags/>
</broker>

The problem is when I am generating an xml file it is generating properly but it is not able read a file.

@XStreamAlias("broker")
public static class Broker {

While file generation it is able to convert Broker class to but not the other way around. All other classes and list are getting correctly mapped but the root @XStreamAlias is not working while reading.

Any pointers as to why will be very helpful.

The exception I am getting: com.thoughtworks.xstream.mapper.CannotResolveClassException: broker

One more question: while calling xStream.fromXML(responseString) how does xStream know which class to use? Say I have two classes with same alias


Solution

  • XStream does not process annotations by default. Add the following before the deserialization of your xml content.

    XStream xstream = new XStream();
    xstream.processAnnotations(Broker.class);