无法将 XML 转换为带有 XStream 的地图
Can not convert XML to Map with XStream
我需要将 XML 文件转换为地图。
根据描述 here 我正在尝试读取一些存储库 XML 文件并将其转换为地图。
但这给了我
[Fatal Error] :1:1: Content is not allowed in prolog.
错误。
我的 XML 文件第一行不包含 ?
符号,我已经删除了它们。
我的密码是
static Map<String,Object> repValues;
static String mainRepositoryXML = "common.xml";
static public void setRepValues(){
String rootPath = Paths.get(".").toAbsolutePath().normalize().toString();
String path = rootPath + File.separator + "src"+ File.separator + "main" + File.separator + "resources" + File.separator + "repository" + File.separator + mainRepositoryXML;
XStream xStream = new XStream(new DomDriver());
xStream.registerConverter(new MapEntryConverter());
xStream.alias("root", Map.class);
repValues = (Map<String,Object>) xStream.fromXML(path);
}
public static class MapEntryConverter implements Converter {
public boolean canConvert(Class clazz) {
return AbstractMap.class.isAssignableFrom(clazz);
}
public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
AbstractMap map = (AbstractMap) value;
for (Object obj : map.entrySet()) {
Map.Entry entry = (Map.Entry) obj;
writer.startNode(entry.getKey().toString());
Object val = entry.getValue();
if ( null != val ) {
writer.setValue(val.toString());
}
writer.endNode();
}
}
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
Map<String, String> map = new HashMap<String, String>();
while(reader.hasMoreChildren()) {
reader.moveDown();
String key = reader.getNodeName(); // nodeName aka element's name
String value = reader.getValue();
map.put(key, value);
reader.moveUp();
}
return map;
}
}
上面的代码实际上是从提到的 above 问题中复制粘贴的。
XML 文件似乎很简单:
<general>
<downloadsPath>C:\Users\myUserDirectory\Downloads</downloadsPath>
<downloadsPathDocker>/Downloads</downloadsPathDocker>
<youTube>https://www.youtube.com/</youTube>
<eBay>https://www.ebay.com/</eBay>
<cherryPic>https://proto.gr/sites/www.proto.gr/files/styles/large_retina/public/images/fruits/cherries.jpg</cherryPic>
<pineApplePic>https://i.pinimg.com/564x/97/86/3e/97863e0a14b69caeea2c92537a81fb1b.jpg</pineApplePic>
<trollPic>https://mallofnorway.com/content/uploads/2020/07/008840118.jpg</trollPic>
</general>
我完全不熟悉将 XML 转换为 Map,反之亦然。没有使用 Java.
处理 XML 文件的经验
我看到了 these questions here(以及更多),但仍然找不到适合我的解决方案。
我做错了什么?
非常感谢任何帮助!
两个修复,fromXML
作为流(不是文字字符串)并且根是 你的 根:
XStream xStream = new XStream(new DomDriver());
xStream.registerConverter(new MapEntryConverter());
xStream.alias("general", Map.class);
Object o = xStream.fromXML(new FileInputStream("/home/josejuan/tmp/y.xml"));
repValues = (Map<String,Object>) o;
有输出
我需要将 XML 文件转换为地图。
根据描述 here 我正在尝试读取一些存储库 XML 文件并将其转换为地图。
但这给了我
[Fatal Error] :1:1: Content is not allowed in prolog.
错误。
我的 XML 文件第一行不包含 ?
符号,我已经删除了它们。
我的密码是
static Map<String,Object> repValues;
static String mainRepositoryXML = "common.xml";
static public void setRepValues(){
String rootPath = Paths.get(".").toAbsolutePath().normalize().toString();
String path = rootPath + File.separator + "src"+ File.separator + "main" + File.separator + "resources" + File.separator + "repository" + File.separator + mainRepositoryXML;
XStream xStream = new XStream(new DomDriver());
xStream.registerConverter(new MapEntryConverter());
xStream.alias("root", Map.class);
repValues = (Map<String,Object>) xStream.fromXML(path);
}
public static class MapEntryConverter implements Converter {
public boolean canConvert(Class clazz) {
return AbstractMap.class.isAssignableFrom(clazz);
}
public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
AbstractMap map = (AbstractMap) value;
for (Object obj : map.entrySet()) {
Map.Entry entry = (Map.Entry) obj;
writer.startNode(entry.getKey().toString());
Object val = entry.getValue();
if ( null != val ) {
writer.setValue(val.toString());
}
writer.endNode();
}
}
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
Map<String, String> map = new HashMap<String, String>();
while(reader.hasMoreChildren()) {
reader.moveDown();
String key = reader.getNodeName(); // nodeName aka element's name
String value = reader.getValue();
map.put(key, value);
reader.moveUp();
}
return map;
}
}
上面的代码实际上是从提到的 above 问题中复制粘贴的。
XML 文件似乎很简单:
<general>
<downloadsPath>C:\Users\myUserDirectory\Downloads</downloadsPath>
<downloadsPathDocker>/Downloads</downloadsPathDocker>
<youTube>https://www.youtube.com/</youTube>
<eBay>https://www.ebay.com/</eBay>
<cherryPic>https://proto.gr/sites/www.proto.gr/files/styles/large_retina/public/images/fruits/cherries.jpg</cherryPic>
<pineApplePic>https://i.pinimg.com/564x/97/86/3e/97863e0a14b69caeea2c92537a81fb1b.jpg</pineApplePic>
<trollPic>https://mallofnorway.com/content/uploads/2020/07/008840118.jpg</trollPic>
</general>
我完全不熟悉将 XML 转换为 Map,反之亦然。没有使用 Java.
处理 XML 文件的经验
我看到了
我做错了什么?
非常感谢任何帮助!
两个修复,fromXML
作为流(不是文字字符串)并且根是 你的 根:
XStream xStream = new XStream(new DomDriver());
xStream.registerConverter(new MapEntryConverter());
xStream.alias("general", Map.class);
Object o = xStream.fromXML(new FileInputStream("/home/josejuan/tmp/y.xml"));
repValues = (Map<String,Object>) o;
有输出