user3246779 user3246779 - 5 days ago 3
Java Question

Converting XML into Java Map<String, Integer>

I'm trying to convert XML into Java code. This XML is in a different file; it matches words with numbers (a probability distribution) and looks like this:

<?xml version="1.0" encoding="UTF-8" ?>
<root>
<Durapipe type="int">1</Durapipe>
<EXPLAIN type="int">2</EXPLAIN>
<woods type="int">2</woods>
<hanging type="int">3</hanging>
<hastily type="int">2</hastily>
<localized type="int">1</localized>
<Schuster type="int">5</Schuster>
<regularize type="int">1</regularize>
<LASR type="int">1</LASR>
<LAST type="int">22</LAST>
<Gelch type="int">2</Gelch>
<Gelco type="int">26</Gelco>
.......
</root>


I'm trying to convert this is to a Java Map, and here's the code I'm using for that:

XStream xstream = new XStream();
@SuppressWarnings("unchecked")
Map<String, Integer> englishCorpusProbDist = (Map<String, Integer>)xstream.fromXML(new File("LocationOfFileOnMyComputer/frequencies.xml"));


Currently, I'm getting the following exception in my console whenever I try to run the above Java code:

Exception in thread "main" com.thoughtworks.xstream.mapper.CannotResolveClassException: root
at com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:79)
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.SecurityMapper.realClass(SecurityMapper.java:71)
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:47)
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:1185)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1169)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1133)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1075)
at ProductAttributeExtractor.main(ProductAttributeExtractor.java:23)


This is a related post, but my problem is an added layer of complexity in that my XML matches Strings with ints, and unfortunately the Java Map cannot use ints, it has to use Integers (which is super frustrating): How to convert XML to java.util.Map and vice versa

Any help you can give would be very appreciated. Thanks in advance!

Answer

You need to register your MapConverter, the class which implements Converter

xstream.registerConverter(new MapEntryConverter());

Hope that helps

Comments