Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodecom.thoughtworks.xstream.mapper.CannotResolveClassException while using xstream under Kettle

Tags:

xstream

kettle

I am using XStream under Kettle to deserialize XML to a Java object and it always gives me the exception: nodecom.thoughtworks.xstream.mapper.CannotResolveClassException

Then I tried my code separately from Kettle and as a simple Java application. and it works fine. For example:

public static void main(String[] args) {
    person p = new person("JJ", "MM");
    XStream xstream = new XStream(new DomDriver());
    xstream.alias("personname", person.class);
    String xml = xstream.toXML(p);
    person pp = (person) xstream.fromXML(xml);
    System.out.println(pp.toString());
}


public class person {

    private String firstname;
    private String lastname;

    public person(String first, String last) {
        this.firstname = first;
        this.lastname = last;
    }

    public String getFirstname() {
        return this.firstname;
    }

    public String getLastname() {
        return this.lastname;
    }

    public void setFirstname(String name) {
        this.firstname = name;
    }

    public void setLastname(String name) {
        this.lastname = name;
    }

}

And this code works fine. However, when I move this code into the Kettle plugin it fails at the step to read meta data from the XML file.

like image 438
Kyleinincubator Avatar asked Feb 29 '12 23:02

Kyleinincubator


2 Answers

I was able to fix the issue. I had to set the class loader for XStream instance I am using to de-serialize the xml string.

So before calling xstream.fromXml(xml) do this:

 xstream.setClassLoader(person.class.getClassLoader());

This will solve the xstream.mapper.CannotResolveClassException exception. This is really weird. Hope this helps.

like image 171
Mr. Ax Avatar answered Oct 25 '22 09:10

Mr. Ax


*xstream.alias("personname", person.class);*

change it to include class name, it will work

xstream.alias("person", person.class);
like image 40
jeff Avatar answered Oct 25 '22 09:10

jeff