public class JAXB2Tester {
public static void main(String[] args) throws JAXBException,IOException {
JAXBContext context = JAXBContext.newInstance(Person.class);
//下面代码演示将对象转变为xml
Marshaller m = context.createMarshaller();
Address address = new Address("China","Beijing","Beijing","ShangDi West","100080");
Person p = new Person(Calendar.getInstance(),"JAXB2",address,Gender.MALE,"SW");
FileWriter fw = new FileWriter("person.xml");
m.marshal(p,fw); //下面代码演示将上面生成的xml转换为对象
FileReader fr = new FileReader("person.xml");
Unmarshaller um = context.createUnmarshaller();
Person p2 = (Person)um.unmarshal(fr);
System.out.println("Country:"+p2.getAddress().getCountry());
}
}@XmlRootElement//表示person是一个根元素
class Person {
@XmlElement
Calendar birthDay; //birthday将作为person的子元素 @XmlAttribute
String name; //name将作为person的的一个属性 public Address getAddress() {
return address;
} @XmlElement
Address address; //address将作为person的子元素 @XmlElement
Gender gender; //gender将作为person的子元素 @XmlElement
String job; //job将作为person的子元素 public Person(){
}
public Person(Calendar birthDay, String name, Address address, Gender gender, String job) {
this.birthDay = birthDay;
this.name = name;
this.address = address;
this.gender = gender;
this.job = job;
}
}enum Gender{
MALE(true),
FEMALE (false);
private boolean value;
Gender(boolean _value){
value = _value;
}
}class Address {
@XmlAttribute
String country;
@XmlElement
String state;
@XmlElement
String city;
@XmlElement
String street;
String zipcode; //由于没有添加@XmlElement,所以该元素不会出现在输出的xml中 public Address() {
} public Address(String country, String state, String city, String street, String zipcode) {
this.country = country;
this.state = state;
this.city = city;
this.street = street;
this.zipcode = zipcode;
}
public String getCountry() {
return country;
}
}运行该程序,我们会得到一个person.xml的文件,如下:<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <person name="JAXB2">
<birthDay>2006-12-28T08:49:27.203+00:00</birthDay>
<address country="China">
<state>Beijing</state>
<city>Beijing</city>
<street>ShangDi West</street>
</address>
<gender>MALE</gender>
<job>SW</job>
</person>
控制台会输出Country:China
public static void main(String[] args) throws JAXBException,IOException {
JAXBContext context = JAXBContext.newInstance(Person.class);
//下面代码演示将对象转变为xml
Marshaller m = context.createMarshaller();
Address address = new Address("China","Beijing","Beijing","ShangDi West","100080");
Person p = new Person(Calendar.getInstance(),"JAXB2",address,Gender.MALE,"SW");
FileWriter fw = new FileWriter("person.xml");
m.marshal(p,fw); //下面代码演示将上面生成的xml转换为对象
FileReader fr = new FileReader("person.xml");
Unmarshaller um = context.createUnmarshaller();
Person p2 = (Person)um.unmarshal(fr);
System.out.println("Country:"+p2.getAddress().getCountry());
}
}@XmlRootElement//表示person是一个根元素
class Person {
@XmlElement
Calendar birthDay; //birthday将作为person的子元素 @XmlAttribute
String name; //name将作为person的的一个属性 public Address getAddress() {
return address;
} @XmlElement
Address address; //address将作为person的子元素 @XmlElement
Gender gender; //gender将作为person的子元素 @XmlElement
String job; //job将作为person的子元素 public Person(){
}
public Person(Calendar birthDay, String name, Address address, Gender gender, String job) {
this.birthDay = birthDay;
this.name = name;
this.address = address;
this.gender = gender;
this.job = job;
}
}enum Gender{
MALE(true),
FEMALE (false);
private boolean value;
Gender(boolean _value){
value = _value;
}
}class Address {
@XmlAttribute
String country;
@XmlElement
String state;
@XmlElement
String city;
@XmlElement
String street;
String zipcode; //由于没有添加@XmlElement,所以该元素不会出现在输出的xml中 public Address() {
} public Address(String country, String state, String city, String street, String zipcode) {
this.country = country;
this.state = state;
this.city = city;
this.street = street;
this.zipcode = zipcode;
}
public String getCountry() {
return country;
}
}运行该程序,我们会得到一个person.xml的文件,如下:<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <person name="JAXB2">
<birthDay>2006-12-28T08:49:27.203+00:00</birthDay>
<address country="China">
<state>Beijing</state>
<city>Beijing</city>
<street>ShangDi West</street>
</address>
<gender>MALE</gender>
<job>SW</job>
</person>
控制台会输出Country:China
private static DownloadTaskItem convertElementToObject(Element element) throws Exception {
String taskId = XMLUtil.getTagValue(element, TAG_TASK_ID);
String taskName = XMLUtil.getTagValue(element, TAG_TASK_NAME);
String targetId = XMLUtil.getTagValue(element, TAG_TARGETID);
int type = Integer.valueOf(XMLUtil.getTagValue(element, TAG_TYPE)).intValue();
int status = Integer.valueOf(XMLUtil.getTagValue(element, TAG_STATUS)).intValue();
String objectName = XMLUtil.getTagValue(element, TAG_OBJECT_NAME);
String objectId = XMLUtil.getTagValue(element, TAG_OBJECTID);
String rootId = XMLUtil.getTagValue(element, TAG_ROOTID);
int destination = Integer.valueOf(XMLUtil.getTagValue(element, TAG_DESTINATION)).intValue();
String destInfo = XMLUtil.getTagValue(element, TAG_DESTINFO);
java.util.Date created = TimeTools.getDateFromFullString(XMLUtil.getTagValue(element, TAG_CREATED));
int inpoint = Integer.valueOf(XMLUtil.getTagValue(element, TAG_INPOINT)).intValue();
int outpoint = Integer.valueOf(XMLUtil.getTagValue(element, TAG_OUTPOINT)).intValue();
int duration = Integer.valueOf(XMLUtil.getTagValue(element, TAG_DURATION)).intValue();
String userName = XMLUtil.getTagValue(element, TAG_USERNAME);
String userRealName = XMLUtil.getTagValue(element, TAG_USER_REALNAME);
DownloadTaskItem item = new DownloadTaskItem();
item.setTaskId(taskId);
item.setTaskName(taskName);
item.setTargetId(targetId);
item.setType(DownloadType.getDownloadType(type));
item.setStatus(DownloadStatus.getDownloadStatus(status));
.....
return item;
}