使用 XStream() 将 LinkedHashMap 和 ConcurrentHashMap 对象转换为 xml 时无法覆盖根元素
Not able to override the root element while converting a LinkedHashMap and ConcurrentHashMap Object to xml using XStream()
我正在尝试使用 XStream() API 将映射对象(HashMap、LinkedHashMap 和 ConcurrentHashMap)转换为 xml 字符串。
在转换 HashMap 时,我可以通过设置像 convertApi.alias("desired-root",Map.class);
这样的别名,将我的映射转换为 xml 和我想要的根元素
而在转换 LinkedHashMap 或 ConcurrentHashMap 时,我没有为我的 xml 获得所需的根,分别像 "linked-hash-map" 或 "concurrent-hash-map" 一样被覆盖
请帮助我在使用 linkedhashmap 或 ConcurrentHashMap 时获得所需的根。提前致谢:)
代码:
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import java.util.AbstractMap;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
public class Mapping {
public static void main(String[] args) {
Map<String,String> mp=new LinkedHashMap<String, String>();
//Map contains: EmpId,Name
mp.put("197","Deepak kumar modi");
mp.put("198","Sweep panorama");
mp.put("199","HD Video");
mp.put("200","My Motorola Mobile");
//System.out.println(mp);
XStream magicApi = new XStream();
magicApi.alias("root", Map.class);
magicApi.registerConverter(new MapEntryConverter());
String xml = magicApi.toXML(mp);
System.out.println("Result of newly formed Xml:");
System.out.println(xml);
}
private static class MapEntryConverter implements Converter{
public void marshal(Object value, HierarchicalStreamWriter hierarchicalStreamWriter, MarshallingContext marshallingContext) {
AbstractMap map = (AbstractMap) value;
for (Object obj : map.entrySet()) {
Map.Entry entry = (Map.Entry) obj;
hierarchicalStreamWriter.startNode(entry.getKey().toString());
hierarchicalStreamWriter.setValue(entry.getValue().toString());
hierarchicalStreamWriter.endNode();
}
}
public Object unmarshal(HierarchicalStreamReader hierarchicalStreamReader, UnmarshallingContext unmarshallingContext) {
Map<String, String> map = new HashMap<String, String>();
while(hierarchicalStreamReader.hasMoreChildren()) {
hierarchicalStreamReader.moveDown();
map.put(hierarchicalStreamReader.getNodeName(), hierarchicalStreamReader.getValue());
hierarchicalStreamReader.moveUp();
}
return map;
}
public boolean canConvert(Class aClass) {
return AbstractMap.class.isAssignableFrom(aClass);
}
}
}
输出:
Result of newly formed Xml:
<linked-hash-map>
<197>Deepak kumar modi</197>
<198>Sweep panorama</198>
<199>HD Video</199>
<200>My Motorola Mobile</200>
</linked-hash-map>
XStream magicApi = new XStream();
magicApi.alias("root", LinkedHashMap.class);
试试这个。
我正在尝试使用 XStream() API 将映射对象(HashMap、LinkedHashMap 和 ConcurrentHashMap)转换为 xml 字符串。
在转换 HashMap 时,我可以通过设置像 convertApi.alias("desired-root",Map.class);
而在转换 LinkedHashMap 或 ConcurrentHashMap 时,我没有为我的 xml 获得所需的根,分别像 "linked-hash-map" 或 "concurrent-hash-map" 一样被覆盖
请帮助我在使用 linkedhashmap 或 ConcurrentHashMap 时获得所需的根。提前致谢:)
代码:
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import java.util.AbstractMap;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
public class Mapping {
public static void main(String[] args) {
Map<String,String> mp=new LinkedHashMap<String, String>();
//Map contains: EmpId,Name
mp.put("197","Deepak kumar modi");
mp.put("198","Sweep panorama");
mp.put("199","HD Video");
mp.put("200","My Motorola Mobile");
//System.out.println(mp);
XStream magicApi = new XStream();
magicApi.alias("root", Map.class);
magicApi.registerConverter(new MapEntryConverter());
String xml = magicApi.toXML(mp);
System.out.println("Result of newly formed Xml:");
System.out.println(xml);
}
private static class MapEntryConverter implements Converter{
public void marshal(Object value, HierarchicalStreamWriter hierarchicalStreamWriter, MarshallingContext marshallingContext) {
AbstractMap map = (AbstractMap) value;
for (Object obj : map.entrySet()) {
Map.Entry entry = (Map.Entry) obj;
hierarchicalStreamWriter.startNode(entry.getKey().toString());
hierarchicalStreamWriter.setValue(entry.getValue().toString());
hierarchicalStreamWriter.endNode();
}
}
public Object unmarshal(HierarchicalStreamReader hierarchicalStreamReader, UnmarshallingContext unmarshallingContext) {
Map<String, String> map = new HashMap<String, String>();
while(hierarchicalStreamReader.hasMoreChildren()) {
hierarchicalStreamReader.moveDown();
map.put(hierarchicalStreamReader.getNodeName(), hierarchicalStreamReader.getValue());
hierarchicalStreamReader.moveUp();
}
return map;
}
public boolean canConvert(Class aClass) {
return AbstractMap.class.isAssignableFrom(aClass);
}
}
}
输出:
Result of newly formed Xml:
<linked-hash-map>
<197>Deepak kumar modi</197>
<198>Sweep panorama</198>
<199>HD Video</199>
<200>My Motorola Mobile</200>
</linked-hash-map>
XStream magicApi = new XStream();
magicApi.alias("root", LinkedHashMap.class);
试试这个。