xstream CannotResolveClassException

梦想与她 提交于 2019-11-28 13:22:31

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

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>

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

XStream xstream = new XStream(new StaxDriver());
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!