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.
分享到:
相关推荐
jaxb xml 转map
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的安装包及插件
activation.jar jaxb1-impl.jar jaxb-api.jar jaxb-impl.jar jaxb-xjc.jar jsr173_1.0_api.jar系列jar下载
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生成XML例子,含有例子和注解解析
有关Maven项目中缺少jaxb-api的异常报错解决,jaxb-core-2.3.0.jar
JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术
jaxb-api jaxb-impl jaxb-xjc jaxws-rt 这四个文件对应的jar包
jaxb-impl-2.1.13.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依赖...
一个JAXB2的基础教程,教你快速运用JAXB通过XSD绑定XML和读写XML