xstream CannotResolveClassException
Asked Answered
C

4

11

I'm trying to use xstream 1.4.2 to convert xml to object. It does work perfectly fine for me until I put the object's class file in a separate package than where the main code runs. Then I get a CannotResolveClassException. I've tried using the setClassLoader method as recommended by others but that doesn't help.

Exception in thread "main" com.thoughtworks.xstream.mapper.CannotResolveClassException: result
at com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:56)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.DynamicProxyMapper.realClass(DynamicProxyMapper.java:55)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.PackageAliasingMapper.realClass(PackageAliasingMapper.java:88)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.ClassAliasingMapper.realClass(ClassAliasingMapper.java:79)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.ArrayMapper.realClass(ArrayMapper.java:74)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.CachingMapper.realClass(CachingMapper.java:45)
at com.thoughtworks.xstream.core.util.HierarchicalStreams.readClassType(HierarchicalStreams.java:29)
at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:133)
at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1052)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1036)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:912)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:903)
at main.readClass(main.java:48)
at main.main(main.java:28)

Answer: xstream expects the xml structure to be relative to the package in which it (the object) originates from. So xstream.alias must be used in order to alias the xml structure.

xstream.alias("something", Something.class);

Otherwise xstream will expect "Something" to be in the default package rather than the package it is a member of.

Countersign answered 3/5, 2012 at 19:31 Comment(3)
Package? What do you mean by package?Epicurean
can you post the xml and the java class you are using to populateRainbow
The fully qualified name works well xstream.alias(...), but how does it work with Annotations ?Lemuel
B
7

When you pass the class name to the alias method you need to pass the fully qualified class name i.e the class name along with its package name.

Example : Suppose if the class Something is in the package com.xyz then you need to pass com.xyz.Something.class as the parameter

Thanks

Broglie answered 31/3, 2013 at 14:24 Comment(0)
B
0

For your question it will be useful

Download Xtream.jar

package businessfunctions;


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

import objectrepository.LoginPojo;
import objectrepository.LoginPojos;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

    public void readRepository(String fNmae){

        // Instantiate XStream Object
        XStream xstream = new XStream(new DomDriver()); 

        // Set Person class into xstream alias
        xstream.alias("loginpojos",LoginPojos.class);
        xstream.alias("loginpojo",LoginPojo.class);

        xstream.addImplicitCollection(LoginPojos.class, "loginpojos");

        // Create a StringBuffer var
        StringBuffer contents = new StringBuffer();

         try {
                // TODO Auto-generated method stub


                // Set BufferReader var "in" to read from PersonXML.xml file
                BufferedReader in = new BufferedReader(new FileReader(repoPath+fNmae));

                String str;

                while ((str = in.readLine()) != null) {
                contents.append(str);
                }

                in.close();

             } catch (IOException e) {
                    System.out.println(e.toString());
             }
            // Checking the StringBuffer variable "contents"
            System.out.println(contents);

            String content = new String();

            // Put all the contents from contents to String variable content --xstream.fromXML()takes
            //only String
            content = contents.toString();

            // Convert content into XML and change and set to obj newPerson of class Person
            LoginPojos loginPojosList = (LoginPojos)xstream.fromXML(content);

            List<LoginPojo> loginPojoList = loginPojosList.getLoginpojos();
            for(LoginPojo newLogiPojo:loginPojoList){
            // Print out the newPerson properties
            System.out.println("Property Label: " + newLogiPojo.getPropLabel());
            System.out.println("Property Name: " + newLogiPojo.getPropName());
            System.out.println("Property Value: " + newLogiPojo.getPropValue());
            }

             }



public static void main(String args[]) {
         try {
             DriverSetup ds= new DriverSetup();
             ds.readRepository("login.xml");  

         }catch(Exception e){
            e.printStackTrace(); 
         }

package objectrepository;

import java.util.ArrayList;
import java.util.List;


public class LoginPojos {

     public List<LoginPojo> loginpojos = new ArrayList<LoginPojo>();

    public List<LoginPojo> getLoginpojos() {
        return loginpojos;
    }

    public void setLoginpojos(List<LoginPojo> loginpojos) {
        this.loginpojos = loginpojos;
    }



}


package objectrepository;

public class LoginPojo {

    private String propLabel;
    private String propName;
    private String propValue;
    public String getPropLabel() {
        return propLabel;
    }
    public void setPropLabel(String propLabel) {
        this.propLabel = propLabel;
    }
    public String getPropName() {
        return propName;
    }
    public void setPropName(String propName) {
        this.propName = propName;
    }
    public String getPropValue() {
        return propValue;
    }
    public void setPropValue(String propValue) {
        this.propValue = propValue;
    }



}


<loginpojos>
<loginpojo>
<propLabel>User name1</propLabel>
<propName>id1</propName>
<propValue>username1</propValue>
</loginpojo>
<loginpojo>
<propLabel>User name</propLabel>
<propName>id</propName>
<propValue>username</propValue>
</loginpojo>
</loginpojos>
Barytes answered 20/3, 2014 at 14:41 Comment(1)
yes, this not resolve my problem but it's useful.TnxVizard
D
0

Spent half an evening trying to resolve similar error.
Passing a driver to xStream miraculously helped.

XStream xstream = new XStream(new StaxDriver());
Dyslexia answered 22/2, 2018 at 16:54 Comment(0)
M
0

In my case I had a mismatch of names. Sender had ConfigWiFi and receiver defined WiFiConfig.

Minded answered 13/10, 2023 at 9:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.