使用JAXB从MAP创建XML [英] Using JAXB to create XML from MAP
问题描述
我想从Java.util.Map创建XML 我将值放在该映射中,并尝试创建XML,该XML的根元素将是可配置的,子元素将从该映射中创建.
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");
XML将如下所示:
<shaonsXML>
<key>shaon</key>
<newKey> newValue </newKey>
</shaonsXML>
我已经看到了使用JAXB的示例,但是这些示例并未像我试图生成的那样创建XML标签.
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
但它会生成此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>
推荐答案
我做到了!使用此示例
从上面的帖子中:创建了此类:
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类:
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);
}
}
创建此CustomMap
Created This CustomMap
@XmlRootElement(name="RootTag")
public class CustomMap extends MapWrapper{
public CustomMap(){
}
}
通过创建XML来测试代码:
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));
并生成了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.
这篇关于使用JAXB从MAP创建XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!