Java 使用JAXB从MAP创建XML

Java 使用JAXB从MAP创建XML,java,xml,jaxb,Java,Xml,Jaxb,我想从Java.util.Map创建XML 我将值放在该映射中,并尝试创建可配置的根元素和从该映射创建的子元素的XML Map mp = new HashMap(); mp.put("key","shaon"): mp.put("newKey","newValue"); XML将类似于: <shaonsXML> <key>shaon</key> <newKey> newValue </newKey> </

我想从Java.util.Map创建XML 我将值放在该映射中,并尝试创建可配置的根元素和从该映射创建的子元素的XML

 Map mp = new HashMap(); 

  mp.put("key","shaon"):

  mp.put("newKey","newValue");
XML将类似于:

<shaonsXML>
  <key>shaon</key>
  <newKey> newValue </newKey>
</shaonsXML>

邵
新价值
我已经看到了使用JAXB的示例,但这些示例并没有像我试图生成的那个样创建XML标记

谁能给我一些链接或建议?提前谢谢

我采纳了这些建议:以及

但它会生成这种XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
    <mapProperty>
        <item>
            <key>KEY1</key>
            <value>SHAON</value>
        </item>
        <item>
            <key>newKEY</key>
            <value>newValue</value>
        </item>
    </mapProperty>
</root>

关键1
邵
纽基
新价值
我做到了!用过的例子

从上面的帖子:创建这个类:

    public class MapAdapter extends XmlAdapter<MapWrapper, Map<String, String>>{

    @Override
    public Map<String, String> unmarshal(MapWrapper v) throws Exception {
        Map<String, String> map = new HashMap<String,String>();//v.toMap();

        return map;
    }

    @Override
    public MapWrapper marshal(Map<String, String> m) throws Exception {
        MapWrapper wrapper = new MapWrapper();

        for(Map.Entry<String, String> entry : m.entrySet()){
             wrapper.addEntry(new JAXBElement<String>(new QName(entry.getKey()), String.class, entry.getValue()));
        }

        return wrapper;
    }

}
通过创建XML测试代码:

private static void writeAsXml(Object o, Writer writer) throws Exception
  {
    JAXBContext jaxb = JAXBContext.newInstance(o.getClass());

    Marshaller xmlConverter = jaxb.createMarshaller();
    xmlConverter.setProperty("jaxb.formatted.output", true);
    xmlConverter.marshal(o, writer);
  }


CustomMap map = new CustomMap();
    map.addEntry("Key1","Value1");
    map.addEntry("Key2","Value2");
    map.addEntry("Key3","Value3");
    map.addEntry("Key4","Value4");
    writeAsXml(map, new PrintWriter(System.out));
并生成了XML:

<RootTag>
    <Key1>Value1</Key1>
    <Key2>Value2</Key2>
    <Key3>Value3</Key3>
    <Key4>Value4</Key4>
</RootTag>

价值1
价值2
价值3
价值4
我只需要编组,所以没有实现解编组部分

我做到了!用过的例子

从上面的帖子:创建这个类:

    public class MapAdapter extends XmlAdapter<MapWrapper, Map<String, String>>{

    @Override
    public Map<String, String> unmarshal(MapWrapper v) throws Exception {
        Map<String, String> map = new HashMap<String,String>();//v.toMap();

        return map;
    }

    @Override
    public MapWrapper marshal(Map<String, String> m) throws Exception {
        MapWrapper wrapper = new MapWrapper();

        for(Map.Entry<String, String> entry : m.entrySet()){
             wrapper.addEntry(new JAXBElement<String>(new QName(entry.getKey()), String.class, entry.getValue()));
        }

        return wrapper;
    }

}
通过创建XML测试代码:

private static void writeAsXml(Object o, Writer writer) throws Exception
  {
    JAXBContext jaxb = JAXBContext.newInstance(o.getClass());

    Marshaller xmlConverter = jaxb.createMarshaller();
    xmlConverter.setProperty("jaxb.formatted.output", true);
    xmlConverter.marshal(o, writer);
  }


CustomMap map = new CustomMap();
    map.addEntry("Key1","Value1");
    map.addEntry("Key2","Value2");
    map.addEntry("Key3","Value3");
    map.addEntry("Key4","Value4");
    writeAsXml(map, new PrintWriter(System.out));
并生成了XML:

<RootTag>
    <Key1>Value1</Key1>
    <Key2>Value2</Key2>
    <Key3>Value3</Key3>
    <Key4>Value4</Key4>
</RootTag>

价值1
价值2
价值3
价值4
我只需要编组,所以没有实现解编组部分