JAXBのMap wih XmlAdapter

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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值