English 中文(简体)
Java XStream 无法解析星群例外
原标题:Java XStream CannotResolveClassException

我试图用 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>

这里是我的代码:

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> 触发例外:

    ServerHandler serverHandler = new ServerHandler();
    serverHandler.readFromXML();
最佳回答

尝试添加一个类服务器以保持您的服务器IP 实例, 并添加此行 :

xstream.alias("servers", Servers.class);
xstream.alias("server", ServerIP.class);

您可以在此找到一个简单的别名教程 : http://x-stre.github.io/alias-tumental.html

问题回答
@XStreamAlias("server")
public class ServerIP {
    private String ip;

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }
}

@Teg指出这个方向。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签