JAXB是很强大的XML <—> Java Class映射工具。很可惜它默认不支持对Hashmap的映射。但我们可以通过使用XmlJavaTypeAdapter来扩展实现,本文介绍详细方法。
首先创建一个带有HashMap的Class
package org.bluedash.demo;
import java.util.HashMap;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {
@XmlJavaTypeAdapter(MyHashMapAdapter.class)
HashMap hashmap;
public Foo() {
hashmap = new HashMap();
}
public HashMap getHashmap() {
return hashmap;
}
public void setHashmap(HashMap hashmap) {
this.hashmap = hashmap;
}
}
我们使用自己的 MyHashMapAdapter.class 来映射 HashMap :
package org.bluedash.demo;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.Map.Entry;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public final class MyHashMapAdapter extends XmlAdapter<MyHashMapType, HashMap> {
@Override
public MyHashMapType marshal(HashMap arg0) throws Exception {
MyHashMapType myHashMapType = new MyHashMapType();
for (Entry entry : (Set<Entry>) arg0.entrySet()) {
MyHashEntryType myHashEntryType = new MyHashEntryType();
myHashEntryType.key = (Integer) entry.getKey();
myHashEntryType.value = (String) entry.getValue();
myHashMapType.entries.add(myHashEntryType);
// myHashMapType = myHashEntryType;
}
return myHashMapType;
}
@Override
public HashMap unmarshal(MyHashMapType arg0) throws Exception {
HashMap hashMap = new HashMap();
for (MyHashEntryType myHashEntryType : (List<MyHashEntryType>) arg0.entries) {
hashMap.put(myHashEntryType.key, myHashEntryType.value);
}
return hashMap;
}
}
里面使用了自定义的 MyHashMapType :
package org.bluedash.demo;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class MyHashMapType {
@XmlElement(required = true)
public List<MyHashEntryType> entries = new ArrayList<MyHashEntryType>();
}
使用 MyHashEntryType 来实现对 HashMap 每一条数据的映射:
package org.bluedash.demo;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType
public class MyHashEntryType {
@XmlAttribute
public Integer key;
@XmlValue
public String value;
}
下面做一个 Demo 来使用一下我们的 Adapter :
package org.bluedash.demo;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class Demo {
public static void main(String[] args) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(Foo.class);
Foo foo = new Foo();
foo.getHashmap().put(1, "One");
foo.getHashmap().put(2, "Two");
foo.getHashmap().put(3, "Three");
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
// Output the generated XML:
marshaller.marshal(foo, System.out);
// Save the output to a foo.xml
File xmlFile = new File("foo.xml");
marshaller.marshal(foo, xmlFile);
// Restore the Foo class from xml file
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Foo createdFoo = (Foo) unmarshaller.unmarshal(xmlFile);
// See the result
System.out.println(createdFoo.hashmap);
}
}
代码运行结果如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<foo>
<hashmap>
<entries key="1">One</entries>
<entries key="2">Two</entries>
<entries key="3">Three</entries>
</hashmap>
</foo>
{1=One, 2=Two, 3=Three}
原文链接:http://www.bluedash.net/spaces/%E4%BD%BF%E7%94%A8JAXB%E6%98%A0%E5%B0%84HashMap
分享到:
相关推荐
JAXB的使用JAXB的使用JAXB的使用JAXB的使用
无需安装,解压后即可。jaxb-api.jar,jaxb-xjc.jar,jaxb-impl.jar,activation.jar等相关jar包在lib文件夹下。
在使用webservice,mule esb等需要jaxb的项目里经常会出现 JAXB 2.0 API is being loaded from the bootstrap classloader这个错误,按照打出的信息Use the endorsed directory mechanism to place jaxb-api.jar in ...
jaxb-api jaxb-impl jar
JAXB的安装包及插件
JAXB的 eclipse插件 JAXB的 eclipse插件 JAXB的 eclipse插件 JAXB的 eclipse插件 JAXB的 eclipse插件
java -jar JAXB2_20060607.jar On Windows, you can just double-click the jar file to execute. Release Notes Browse the release notes online, including what's new. Technical Support Please subscribe to...
有关Maven项目中缺少jaxb-api的异常报错解决,jaxb-api-2.3.0.jar
Jaxb annotation 使用 Jaxb annotation初步使用
赠送jar包:jackson-module-jaxb-annotations-2.2.3.jar; 赠送原API文档:jackson-module-jaxb-annotations-2.2.3-javadoc.jar; 赠送源代码:jackson-module-jaxb-annotations-2.2.3-sources.jar; 赠送Maven依赖...
JAXB能够使用Jackson对JAXB注解的支持实现(jackson-module-jaxb-annotations),既方便生成XML,也方便生成JSON,这样一来可以更好的标志可以转换为JSON对象的JAVA类。JAXB允许JAVA人员将JAVA类映射为XML表示方式
使用jaxb生成XML例子,含有例子和注解解析
JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术
jaxb-api jaxb-impl jaxb-xjc jaxws-rt 这四个文件对应的jar包
现在很多maven仓库均没有此jar jaxb-api-2.3.0.jar jaxb-core-2.3.0.jar
赠送jar包:jackson-module-jaxb-annotations-2.7.8.jar; 赠送原API文档:jackson-module-jaxb-annotations-2.7.8-javadoc.jar; 赠送源代码:jackson-module-jaxb-annotations-2.7.8-sources.jar; 赠送Maven依赖...
有关Maven项目中缺少jaxb-api的异常报错解决,jaxb-core-2.3.0.jar
一个JAXB2的基础教程,教你快速运用JAXB通过XSD绑定XML和读写XML
我刚写好的 一个简单的小程序 动行里面的read文件 是在idea 中用jaxb 读xml中数据
jaxb-2.2.jar jaxb jax