使用JAXB映射HashMap_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 使用JAXB映射HashMap

使用JAXB映射HashMap

 2011/11/25 9:18:42  阿男bluedash  http://bluedash.iteye.com  我要评论(0)
  • 摘要:JAXB是很强大的XML<-->JavaClass映射工具。很可惜它默认不支持对Hashmap的映射。但我们可以通过使用XmlJavaTypeAdapter来扩展实现,本文介绍详细方法。首先创建一个带有HashMap的Class:packagenet.bluedash;importjava.util.HashMap;importjavax.xml.bind.annotation.XmlAccessType;importjavax.xml.bind.annotation
  • 标签:has 使用 Map Hash
JAXB是很强大的XML <--> Java Class映射工具。很可惜它默认不支持对Hashmap的映射。但我们可以通过使用XmlJavaTypeAdapter来扩展实现,本文介绍详细方法。

首先创建一个带有HashMap的Class:

package net.bluedash;

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 net.bluedash;

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 net.bluedash;

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 net.bluedash;

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 net.bluedash;

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 App {

	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}


我将上面的例子放在了github上面,可以取下来动手玩玩看:

% git clone git://github.com/liweinan/jaxb-hashmap.git


执行下述Maven命令运行:

mvn exec:java -Dexec.mainClass="net.bluedash.App"
发表评论
用户名: 匿名