根(rootelement)类
package com.xmlElement.poeple;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class people {
private String name;
private int age;
private String sex;
@XmlElement
private List<Childdren> child;
public List<Childdren> getChild() {
return (List<Childdren>) child;
}
public void setChild(List<Childdren> child) {
this.child =(List<Childdren>) child;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
子类 子类设置它的子类为一个xml元素时如果要加get/set方法,则需要加上这个注解@XmlAccessorType(XmlAccessType.FIELD)
package com.xmlElement.poeple;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
@XmlAccessorType(XmlAccessType.FIELD)
public class Childdren {
private String name;
private int age;
private String sex;
@XmlElement
private List<GrandChilddren> grandChild;
public List<GrandChilddren> getGrandChild() {
return (List<GrandChilddren>)grandChild;
}
public void setGrandChild(List<GrandChilddren> grandChild) {
this.grandChild = (List<GrandChilddren>) grandChild;
}
public Childdren(){
}
public Childdren(String name,int age,String sex){
this.name=name;
this.age=age;
this.sex=sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
子类的子类
package com.xmlElement.poeple;
import javax.xml.bind.annotation.XmlAccessorType;
@XmlAccessorType
public class GrandChilddren {
private String name;
private int age;
private String sex;
public GrandChilddren(){
}
public GrandChilddren(String name,int age,String sex){
this.name=name;
this.age=age;
this.sex=sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
测试程序
package com.test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.Result;
import com.xmlElement.poeple.Childdren;
import com.xmlElement.poeple.GrandChilddren;
import com.xmlElement.poeple.people;
public class People_BAXB_Test {
public static void main(String[] args) throws JAXBException {
// TODO Auto-generated method stub
people p=new people();
p.setName("张三非一般");
p.setAge(21);
p.setSex("男");
Childdren child1=new Childdren("张三儿子非一般",1,"男");
Childdren child2=new Childdren("张三女儿非一般",1,"女");
GrandChilddren grandchildren1=new GrandChilddren("张三儿子的儿子非一般:张武",0,"未出生不知道");
GrandChilddren grandchildren2=new GrandChilddren("张三儿子的女儿非一般:张非",0,"未出生不知道");
List l1=new ArrayList();
l1.add(grandchildren1);
l1.add(grandchildren2);
child1.setGrandChild(l1);
List<Childdren> childdren = new ArrayList<Childdren>();
childdren.add(child1);
childdren.add(child2);
p.setChild(childdren);
JAXBContext jc=JAXBContext.newInstance(p.getClass());
Marshaller peopleMarshaller = jc.createMarshaller();
Unmarshaller peopleUnMarshaller= jc.createUnmarshaller();
peopleMarshaller.setProperty(Marshaller.JAXB_ENCODING, "GB2312");
peopleMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);;
System.out.println("=====================Marshaller start!==================");
peopleMarshaller.marshal(p,System.out);
System.out.println();
//将xml写入到sring数组
ByteArrayOutputStream os = new ByteArrayOutputStream();
peopleMarshaller.marshal(p, os);
String xmlStr = os.toString();
//System.out.println(xmlStr);
System.out.println("=====================UnMarshaller start!==================");
ByteArrayInputStream ins=new ByteArrayInputStream(xmlStr.getBytes());
people p1=(people)peopleUnMarshaller.unmarshal(ins);
System.out.println(p1.getName());
}
}