Using JAXB to create XML from MAP

别说谁变了你拦得住时间么 提交于 2021-02-08 08:49:16

问题


I want to create XML from Java.util.Map I am putting values in that map and trying to create XML which root element will be configurable and child elements will be created from that map.

 Map mp = new HashMap(); 

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

  mp.put("newKey","newValue");

And the XML will be like:

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

I have seen example which uses JAXB, but those example does not create XML tag as I am trying to generate.

Can anyone give me some link or suggestion ? Thanks in advance!

I have followed These suggestions: this and this

but its generating this 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:


I did it! used this Example

From that above post: Created this class:

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

}

MapWrapper Class:

@XmlType
public class MapWrapper{
    private List<JAXBElement<String>> properties = new ArrayList<>();

    public MapWrapper(){

    }

    @XmlAnyElement
    public List<JAXBElement<String>> getProperties() {
        return properties;
    }
    public void setProperties(List<JAXBElement<String>> properties) {
        this.properties = properties;
    }
    public void addEntry(JAXBElement<String> prop){
        properties.add(prop);
    }

    public void addEntry(String key, String value){
        JAXBElement<String> prop = new JAXBElement<String>(new QName(key), String.class, value);
        addEntry(prop);
    }

}

Created This CustomMap

@XmlRootElement(name="RootTag")
public class CustomMap extends MapWrapper{
    public CustomMap(){

    }
}

Test Code by creating 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));

And produced XML:

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

I needed only Marshaling So did not implement that Unmarshal part.



来源:https://stackoverflow.com/questions/41893912/using-jaxb-to-create-xml-from-map

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!