1

I am using XStream to convert XML string to Java Object.

I have a huge set of data but I am posting the minimal code below:

XStream xstream = new XStream(new StaxDriver());
xstream.alias("data", DetList.class);
xstream.alias("i", Details.class);
String s = new String("<data>\n"
           +"\t<i Name='asia' type='continent' id='11'></i>\n"
           +"\t<i Name='africa' type='continent' id='12'></i>\n"
           +"\t<i Name='japan' type='country' id='13'></i>\n"
           +"</data>");
System.out.println(s);
DetList data = (DetList) xstream.fromXML(s);

When I debug, data is always null.

Here is my DetList class:

public class DetList {
    private List<Details> detlist;

    public List<Details> getDetlist() {
        return detlist;
    }

    public void setDetlist(List<Details> detlist) {
        this.detlist = detlist;
    }
}

And my Details class:

public class Details {

    private String Name;
    private String type;
    private String id;

    //Getters and Setters are here.
}

data is null which is supposed to contain the list of i.

How can I get it to work?

6
  • Do you get any errors? Commented Dec 2, 2016 at 0:00
  • @GOXR3PLUS No, there is no errors. I'm sure that I'm missing something but not sure what. Commented Dec 2, 2016 at 0:02
  • You have the getters and setters for the name variable? Also it should be name istead of Name Commented Dec 2, 2016 at 0:04
  • I did that, it does not have any affect. Commented Dec 2, 2016 at 0:06
  • Have a look (x-stream.github.io/annotations-tutorial.html ) if you want to use @attributes , your question has multiple errors . One is that it is expecting items instead of attributes in the <i> </i> element. Commented Dec 2, 2016 at 0:30

1 Answer 1

1

Here is your error in case you are interested . I will replace it as soon as i find the answer :

Exception in thread "main" com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$UnknownFieldException: No such field application.DetList.i
---- Debugging information ----
message             : No such field application.DetList.i
field               : i
class               : application.DetList
required-type       : application.DetList
converter-type      : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path                : /data/i
line number         : 2
version             : 1.4.9
-------------------------------
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.handleUnknownField(AbstractReflectionConverter.java:524)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:375)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:281)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70)
    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:1230)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1214)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1085)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1076)
    at application.Tester.main(Tester.java:15)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.