我正在尝试将XML转换为Java代码。此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>
我正在尝试将其转换为Java Map,这是我正在使用的代码:
XStream xstream = new XStream();
@SuppressWarnings("unchecked")
Map<String, Integer> englishCorpusProbDist = (Map<String, Integer>)xstream.fromXML(new File("LocationOfFileOnMyComputer/frequencies.xml"));
目前,每当我尝试运行上述Java代码时,我在控制台中都会遇到以下异常:
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)
这是一个相关的帖子,但我的问题是增加了一层复杂性,因为我的XML与字符串匹配,不幸的是Java Map不能使用整数,它必须使用整数(这是非常令人沮丧的):{{ 3}}
非常感谢您提供的任何帮助。提前谢谢!
答案 0 :(得分:2)
您需要注册MapConverter,即实现Converter
的类xstream.registerConverter(new MapEntryConverter());
希望有所帮助
答案 1 :(得分:0)
Underscore-java库可以将xml转换为hashmap,反之亦然。我是该项目的维护者。 Live 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}
}
}