0

I need to convert this 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 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

  • You to have to register Map first. – PythonLearner Dec 03 '19 at 14:35
  • https://stackoverflow.com/questions/1537207/how-to-convert-xml-to-java-util-map-and-vice-versa – Varun Dec 03 '19 at 14:37
  • Does this answer your question? [How to convert XML to java.util.Map and vice versa](https://stackoverflow.com/questions/1537207/how-to-convert-xml-to-java-util-map-and-vice-versa) – PythonLearner Dec 03 '19 at 14:38
  • I've seen this thread, it doesn't help. I'm getting the exception despite doing as people in the thread you guys mentioned do. I guess I'm unaware of something related to XStream that might solve the issue – Max Urbanowicz Dec 03 '19 at 14:57

2 Answers2

1

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.

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.

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;
}
}

with this output

Now you can use res object to generate the HashMap you prefer

  • This is good, it works. I think what's left to do is maybe a method in Response class returning generated HashMap. Thank you man :) – Max Urbanowicz Dec 04 '19 at 07:59
0

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. Many generators can be found on the Internet for example. Then you need to use xjc executable found in your jdk directory. It creates pojo classes in chosen destination, using those classes you can map data from XML to them with JAXB.