`

JAXBのMap wih XmlAdapter

    博客分类:
  • JAXB
 
阅读更多
We usually use List<Item> it is complete support.
and We also usually use Map<item.id,Item>, this hasn't original support.

package ycl.learn.xml.jaxb.map;

import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;


@XmlRootElement
public class Employee {
	  
	
	@XmlJavaTypeAdapter(MapRealXmlAdapter.class)
	@XmlElement(name="familys")
	public Map<String,Family> familiesmap = new HashMap<String,Family>();
	 
}


package ycl.learn.xml.jaxb.map;

import javax.xml.bind.annotation.XmlAttribute;

 

public class Family {

	private String name;
	private String value;  
	@XmlAttribute
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@XmlAttribute
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	 
	 
	public String toString(){
		return name+":"+value;
	}
	
}



package ycl.learn.xml.jaxb.map;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlMixed;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public class MapRealXmlAdapter extends XmlAdapter<MapRealXmlAdapter.AdaptedHashMap,Map<String,Family>> {
  
	
	@Override
	public AdaptedHashMap marshal(Map<String,Family> v) throws Exception {
		AdaptedHashMap adaptedMap = new AdaptedHashMap();
	        for(Map.Entry<String,Family> entry : v.entrySet()) {
	        	adaptedMap.getFamily().add(entry.getValue());
	        }
	        return adaptedMap;

	}

	@Override
	public Map<String,Family> unmarshal(AdaptedHashMap v) throws Exception { 
		Map<String, Family> mappings = new HashMap<String, Family>();
        for(Family mapping : v.getFamily()) {
            mappings.put(mapping.getName(), mapping);
        }
        return mappings;
	}
	 
	public static class AdaptedHashMap{ 
		 
		
		private List<Family> family = new ArrayList<Family>();

		@XmlElement(name="family")
		public List<Family> getFamily() {
			return family;
		}

		public void setFamily(List<Family> family) {
			this.family = family;
		} 
		
		
	}  


}



package ycl.learn.xml.jaxb.map;

import java.io.File;
import java.io.FileNotFoundException;

import javax.xml.bind.JAXBException;

import ycl.learn.xml.jaxb.JAXBUtil;

public class JAXBUtilTest { 
	
	private static final String FILE_NAME_emps = "employees.xml";
	private static final String FILE_NAME_emps_copy = "employees_copy.xml";
	/**
	 * @param args
	 * @throws JAXBException 
	 * @throws FileNotFoundException 
	 */
	public static void main(String[] args) throws FileNotFoundException, JAXBException { 
		
		JAXBUtil<Employee> ju = new JAXBUtil<Employee>(); 
		Employee empant = ju.unmarshal(Employee.class, new File(FILE_NAME_emps));
		System.out.println("reader success lll000"); 
		//System.out.println(empant.family);
		System.out.println(empant.familiesmap);
		ju.marshal(empant, new File(FILE_NAME_emps_copy)); 
	}

}



<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>
	<familys>
		<family name="a"></family>
		<family name="b" value="ccc"></family>
		<family name="c" value="cccc" extendss="a"></family>
	</familys>
</employee>


this can be marshal and unmarshal this familys to map/ot xml.
but the export xml is

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>
	<familys>
		<family value="ccc" name="b" />
		<family value="cccc" name="c" />
		<family name="a" />
	</familys>
</employee>


there is the difference is the order, we know the map has no order, but the key's hascode.
so If we want to export the order datas, we can do two ways.
one way:
1. rewrite the key's compared method.
2. use sortedHashMap.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics