11

我正在尝试使用 xstream 1.4.2 将 xml 转换为对象。直到我将对象的类文件放在一个单独的包中,而不是主代码运行的地方,它对我来说确实工作得很好。然后我得到一个CannotResolveClassException。我试过使用其他人推荐的 setClassLoader 方法,但这没有帮助。

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)

答案: xstream 期望 xml 结构与它(对象)源自的包相关。因此必须使用 xstream.alias 来为 xml 结构设置别名。

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

否则 xstream 会期望“Something”在默认包中,而不是它所属的包中。

4

3 回答 3

7

当您将类名传递给别名方法时,您需要传递完全限定的类名,即类名及其包名。

示例:假设如果类 Something 在包 com.xyz 中,那么您需要将 com.xyz.Something.class 作为参数传递

谢谢

于 2013-03-31T14:24:23.967 回答
0

对于您的问题,它将很有用

下载 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>
于 2014-03-20T14:41:16.470 回答
0

花了半个晚上试图解决类似的错误。
将驱动程序传递给 xStream 奇迹般地有所帮助。

XStream xstream = new XStream(new StaxDriver());
于 2018-02-22T16:54:54.943 回答