简体   繁体   中英

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!

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

xstream.registerConverter(new MapEntryConverter());

Hope that helps

Underscore-java library can convert xml to hashmap and vice verse. I am the maintainer of the project. Live example

Code example:

import com.github.underscore.lodash.U;

public class Main {
    public static void main(String[] args) {
      String xml = "<?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>";

    String result = U.fromXmlWithoutAttributes(xml).toString();
    // {Durapipe=1, EXPLAIN=2, woods=2, hanging=3, hastily=2, localized=1, Schuster=5, regularize=1, LASR=1, LAST=22, Gelch=2, Gelco=26}

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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