我正在尝试使用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”放在默认包中,而不是它所属的包。
答案 0 :(得分:7)
当您将类名传递给别名方法时,您需要传递完全限定的类名,即类名及其包名。
示例:假设如果类Something在com.xyz包中,则需要将com.xyz.Something.class作为参数传递
由于
答案 1 :(得分: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>
答案 2 :(得分:0)
花了半个晚上试图解决类似的错误。
将驱动程序传递给xStream奇迹般地帮助了。
XStream xstream = new XStream(new StaxDriver());