简体   繁体   English

Java - 使用 XStream 的 XML 到 HashMap - 无法解析类异常:响应

[英]Java - XML to HashMap using XStream - CannotResolveClassException: Response

I need to convert this XML:我需要转换这个 XML:

<?xml version="1.0" encoding="utf-8"?>
<Response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Status>0</Status>
    <Credit>98</Credit>
</Response>

to a Java HashMap using XStream:使用 XStream 到 Java HashMap:

XStream xStream = new XStream();
xStream.alias("hashmap", java.util.HashMap.class);
HashMap<String, Object> myHashmap = (HashMap<String, Object>) xStream.fromXML(myXmlAsString);

but this exception is thrown:但是抛出这个异常:

com.thoughtworks.xstream.mapper.CannotResolveClassException: Response

My question is: what am I doing wroing?我的问题是:我在做什么? I've browsed similar threads here but none seem to help.我在这里浏览过类似的主题,但似乎没有任何帮助。 Any advice appreciated任何建议表示赞赏

I'm not sure this is the exact answer, but let's try.我不确定这是确切的答案,但让我们尝试一下。

IMO the error is trying to map an XML directly into an HashMap, without telling XStream how to do it. IMO 错误试图将 XML 直接映射到 HashMap,而没有告诉 XStream 如何做到这一点。

For this reason I suggest to generate a Class which reflects xml schema and a second Class which maps the first one into a Map.出于这个原因,我建议生成一个反映 xml 模式的类和一个将第一个类映射到 Map 的第二个类。

For example, I put your code in this simple class:例如,我将您的代码放在这个简单的类中:

enter code herepackage com.stackoverflow.test.xstream_xml_to_map;

import java.io.File;

import com.thoughtworks.xstream.XStream;

public class App {

public static void main(String[] args) {
    XStream xStream = new XStream();
    File f = new File(App.class.getClassLoader().getResource("provided.xml").getFile());
    xStream.alias("Response", Response.class);
    Response res = (Response) xStream.fromXML(f);
    System.out.println("Credit: "+res.getCredit());
    System.out.println("Status: "+res.getStatus());
}
}

using this Response class:使用这个响应类:

package com.stackoverflow.test.xstream_xml_to_map;

import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("Response")
public class Response {

private String Status = new String();

private String Credit = new String();

public String getStatus() {
    return Status;
}

public String getCredit() {
    return Credit;
}
}

有了这个输出

Now you can use res object to generate the HashMap you prefer现在您可以使用 res 对象生成您喜欢的 HashMap

Okay, I have much better solution now, provided to me by a coworker.好的,我现在有更好的解决方案,由同事提供给我。

First of all, you need to generate an XML Schema Definition - XSD file.首先,您需要生成一个 XML Schema Definition - XSD 文件。 Many generators can be found on the Internet for example.例如,可以在 Internet 上找到许多生成器。 Then you need to use xjc executable found in your jdk directory.然后您需要使用在 jdk 目录中找到的 xjc 可执行文件。 It creates pojo classes in chosen destination, using those classes you can map data from XML to them with JAXB.它在选定的目的地创建 pojo 类,使用这些类,您可以使用 JAXB 将数据从 XML 映射到它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM