Converting XML into Java Map<String, Integer>

偶尔善良 提交于 2019-12-02 21:59:31

问题


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!


回答1:


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

xstream.registerConverter(new MapEntryConverter());

Hope that helps




回答2:


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}

    }
}


来源:https://stackoverflow.com/questions/25085399/converting-xml-into-java-mapstring-integer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!