源码地址:http://files.cnblogs.com/files/xiluhua/JAXB-test.rar
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>www.webservice.com</groupId> <artifactId>JAXB</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>JAXB</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.2.3</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.5</version> </dependency> </dependencies> </project>
main JaxbTest1:
package jaxb; import java.io.StringReader; import java.io.StringWriter; import java.io.Writer; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import javax.xml.transform.stream.StreamSource; import DTO.Boss; import DTO.Worker; public class JaxbTest1 { public static void main(String[] args) throws JAXBException, ParseException { Boss boss = new Boss(); boss.setID(1); boss.setNAME("李四"); boss.setAGE(20); boss.setNICK_NAME("nickName"); boss.setEMAIL("abc@163.com"); boss.setZIP("000000"); boss.setADDRESS("中华人民共和国"); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); boss.setBIRTH(dateFormat.parse("1990-09-09 09:09:09")); boss.setJOB("教师"); Worker worker1 = new Worker(); worker1.setID(1); worker1.setNAME("工人1"); Worker worker2 = new Worker(); worker2.setID(2); worker2.setNAME("工人2"); List<Worker> workers = new ArrayList<Worker>(); workers.add(worker1); workers.add(worker2); boss.setWORKER(workers); JAXBContext context = JAXBContext.newInstance(Boss.class); Marshaller marsheller = context.createMarshaller(); Unmarshaller unmarsheller = context.createUnmarshaller(); // marshal System.err.println("marshal:"); Writer writer = new StringWriter(); marsheller.marshal(boss, writer); String xml = writer.toString(); System.out.println(xml); System.out.println(); // unmarshal xml = "<REQUEST><REAL_NAME>zhangsan</REAL_NAME></REQUEST>"; JAXBElement<Boss> root = unmarsheller.unmarshal(new StreamSource(new StringReader(xml)),Boss.class); Boss boss2 = root.getValue(); System.err.println("unmarshal:"); System.out.println(boss2.getNAME()); } }
Person:
package DTO; import java.util.Date; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlSeeAlso; import javax.xml.bind.annotation.XmlTransient; import javax.xml.bind.annotation.XmlType; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import adapter.DateTimeAdapter; @XmlAccessorType(XmlAccessType.FIELD) @XmlSeeAlso({Employee.class,Boss.class}) @XmlType(propOrder = { "ID", "NAME", "AGE", "BIRTH", "EMAIL", "ZIP", "ADDRESS" }) public class Person { @XmlAttribute public int ID; @XmlElement(name = "REAL_NAME") public String NAME; @XmlTransient public String NICK_NAME; @XmlElement public int AGE; @XmlElement @XmlJavaTypeAdapter(value = DateTimeAdapter.class) public Date BIRTH; @XmlTransient public String JOB; public String EMAIL; public String ZIP; public String ADDRESS; public int getID() { return ID; } public void setID(int iD) { ID = iD; } public String getNAME() { return NAME; } public void setNAME(String nAME) { NAME = nAME; } public int getAGE() { return AGE; } public void setAGE(int aGE) { AGE = aGE; } public Date getBIRTH() { return BIRTH; } public void setBIRTH(Date bIRTH) { BIRTH = bIRTH; } public String getEMAIL() { return EMAIL; } public void setEMAIL(String eMAIL) { EMAIL = eMAIL; } public String getZIP() { return ZIP; } public void setZIP(String zIP) { ZIP = zIP; } public String getADDRESS() { return ADDRESS; } public void setADDRESS(String aDDRESS) { ADDRESS = aDDRESS; } public String getJOB() { return JOB; } public void setJOB(String jOB) { JOB = jOB; } public String getNICK_NAME() { return NICK_NAME; } public void setNICK_NAME(String nICK_NAME) { NICK_NAME = nICK_NAME; } }
Boss:
@XmlRootElement(name = "REQUEST") public class Boss extends Person{ @XmlElementWrapper(name="WORKERS") @XmlElement(name="WORKER") public List<Worker> WORKER; public List<Worker> getWORKER() { return WORKER; } public void setWORKER(List<Worker> wORKER) { WORKER = wORKER; } }
Worker:
package DTO;
public class Worker extends Person{
}
DateAdapter:
package adapter;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class DateAdapter extends XmlAdapter<String, Date> {
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
@Override
public String marshal(Date v) throws Exception {
return dateFormat.format(v);
}
@Override
public Date unmarshal(String v) throws Exception {
return dateFormat.parse(v);
}
}
DateTimeAdapter:
package adapter; import java.text.SimpleDateFormat; import java.util.Date; import javax.xml.bind.annotation.adapters.XmlAdapter; public class DateTimeAdapter extends XmlAdapter<String, Date> { private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override public String marshal(Date v) throws Exception { return dateFormat.format(v); } @Override public Date unmarshal(String v) throws Exception { return dateFormat.parse(v); } }