如何将XML转换为java.util.Map,反之亦然

前端之家收集整理的这篇文章主要介绍了如何将XML转换为java.util.Map,反之亦然前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在搜索一个轻量级API(最好是单个类)来转换
Map<String,String> map = new HashMap<String,String();

到xml,反之亦然,将XML转换回Map。

例:

Map<String,String();
map.put("name","chris");
map.put("island","faranga");

MagicAPI.toXML(map,"root");

结果:

<root>
  <name>chris</chris>
  <island>faranga</island>
</root>

然后回来:

Map<String,String> map = MagicAPI.fromXML("...");

我不想使用JAXBJSON conversion API.它不必照顾嵌套地图或属性或任何其他,只是那个简单的情况。
有什么建议么?

编辑:我创建了一个工作副本&粘贴样品。感谢fvuMichal Bernhard

Download latest XStream framework,’core only’就够了。

Map<String,Object> map = new HashMap<String,Object>();
map.put("name","faranga");

// convert to XML
XStream xStream = new XStream(new DomDriver());
xStream.alias("map",java.util.Map.class);
String xml = xStream.toXML(map);

// from XML,convert back to map
Map<String,Object> map2 = (Map<String,Object>) xStream.fromXML(xml);

不需要转换器或任何其他。只是xstream-x.y.z.jar就够了。

XStream!

更新:我在注释中添加了请求的解组件。

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.Map;

public class Test {

    public static void main(String[] args) {

        Map<String,String>();
        map.put("name","chris");
        map.put("island","faranga");

        XStream magicApi = new XStream();
        magicApi.registerConverter(new MapEntryConverter());
        magicApi.alias("root",Map.class);

        String xml = magicApi.toXML(map);
        System.out.println("Result of tweaked XStream toXml()");
        System.out.println(xml);

        Map<String,String> extractedMap = (Map<String,String>) magicApi.fromXML(xml);
        assert extractedMap.get("name").equals("chris");
        assert extractedMap.get("island").equals("faranga");

    }

    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>();

            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;
        }

    }

}
原文链接:https://www.f2er.com/xml/293966.html

猜你在找的XML相关文章