我试图用 XStream 将 XML 文件解析为对象, 但我得到了这个例外 :
Exception in thread "main" com.thoughtworks.xstream.mapper.CannotResolveClassException: servers 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) [...]
<强> 这是我的XML: 强>
<servers>
<server>
<ip>10.196.113.27</ip>
</server>
<server>
<ip>10.196.113.31</ip>
</server>
</servers>
这里是我的代码: manger>
public class ServerIP {
private String ip;
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
}
public class ServerHandler {
private String fileName = "servers.xml";
private String path = "J:\workspace\LOG730\src\Q3\";
private XStream xstream = new XStream(new DomDriver());
public void readFromXML() {
try {
FileInputStream fis = new FileInputStream(path + fileName);
ServerIP server = (ServerIP) xstream.fromXML(fis, new ServerIP());
System.out.println("Host: " + server.getIp());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
<% 1> 触发例外: % 1>
ServerHandler serverHandler = new ServerHandler();
serverHandler.readFromXML();