sometimes i also doubt what XmlAdapter do.
1. format the String to anyType you want.[data]
2. split the String, and set every part to Object.[interesting]
3. retrieve the ref's datas to owner Object properties.
...
package ycl.learn.xml.jaxb.list;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlRootElement
public class Employee {
@XmlJavaTypeAdapter(MapXmlAdapter.class)
@XmlElement(name="family")
public List<Family> family;
}
package ycl.learn.xml.jaxb.list;
import javax.xml.bind.annotation.XmlAttribute;
public class Family {
private String name;
private String value;
private Family family;
@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 Family getFamily() {
return family;
}
public void setFamily(Family family) {
this.family = family;
}
public String toString(){
return name+":"+value+"["+family+"]";
}
}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>
<family name="a" ></family>
<family name="b" value="ccc"></family>
<family name="c" value="cccc" extendss="a"></family>
</employee>
It is very easy function, just set extendss's family to it's family.
package ycl.learn.xml.jaxb.list;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class MapXmlAdapter extends XmlAdapter<MapXmlAdapter.AdaptedHashMap,Family> {
List<Family> families = new ArrayList<Family>();
Map<String,Family> mmfa = new HashMap<String,Family>();
@Override
public AdaptedHashMap marshal(Family v) throws Exception {
AdaptedHashMap ae = new AdaptedHashMap();
ae.setName(v.getName());
ae.setValue(v.getValue());
Family family = v.getFamily();
if(family!=null){
String extendss = family.getName();
ae.setExtendss(extendss);
}
return ae;
}
@Override
public Family unmarshal(AdaptedHashMap v) throws Exception {
String extendss = v.getExtendss();
Family mm = new Family();
mm.setName(v.getName());
mm.setValue(v.getValue());
if(extendss != null){
if(mmfa.containsKey(extendss)){
mm.setFamily(mmfa.get(extendss));
}else{
System.out.println("extendss is not exists");
}
}
mmfa.put(mm.getName(), mm);
return mm;
}
public static class AdaptedHashMap extends Family{
private String extendss;
@XmlAttribute
public String getExtendss() {
return extendss;
}
public void setExtendss(String extendss) {
this.extendss = extendss;
}
}
}
package ycl.learn.xml.jaxb.list;
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),new MapXmlAdapter());
System.out.println("reader success lll000");
System.out.println(empant.family);
ju.marshal(empant, new File(FILE_NAME_emps_copy));
}
}
package ycl.learn.xml.jaxb;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class JAXBUtil<T> {
@SuppressWarnings("unchecked")
public T unmarshal(Class<T> clazz, InputStream is) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(clazz);
Unmarshaller un = context.createUnmarshaller();
return (T) un.unmarshal(is);
}
@SuppressWarnings("unchecked")
public T unmarshal(Class<T> clazz, InputStream is,XmlAdapter adapter) throws JAXBException {
if(adapter == null){
return unmarshal(clazz,is);
}else{
JAXBContext context = JAXBContext.newInstance(clazz);
Unmarshaller un = context.createUnmarshaller();
un.setAdapter(adapter);
return (T) un.unmarshal(is);
}
}
public T unmarshal(Class<T> clazz, File file) throws JAXBException, FileNotFoundException {
return unmarshal(clazz,new FileInputStream(file));
}
public T unmarshal(Class<T> clazz, File file,XmlAdapter adapter) throws JAXBException, FileNotFoundException {
return unmarshal(clazz,new FileInputStream(file),adapter);
}
public void marshal(T element,OutputStream os) throws JAXBException{
JAXBContext jc = JAXBContext.newInstance(element.getClass());
Marshaller m = jc.createMarshaller();
m.marshal(element, os);
}
public void marshal(T element,OutputStream os,XmlAdapter adapter) throws JAXBException{
if(adapter == null){
marshal(element,os);
}else{
JAXBContext jc = JAXBContext.newInstance(element.getClass());
Marshaller m = jc.createMarshaller();
m.setAdapter(adapter);
m.marshal(element, os);
}
}
public void marshal(T element, File output) throws FileNotFoundException, JAXBException{
marshal(element,new FileOutputStream(output));
}
public void marshal(T element, File output,XmlAdapter adapter) throws FileNotFoundException, JAXBException{
marshal(element,new FileOutputStream(output),adapter);
}
}
JAXBUtils is complex now, hahah, more function, more complex, this is the rule.