1.Student类:
package test;
import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="stu")//根节点名
public class Student {
private int age;
private int id;
private String name;
private List<Book> books;
private String[] names;
@XmlAttribute//封装成stu的属性,而不是子节点
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement(name="age")//设置name属性改变xml节点名称,不设置默认为变量名
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@XmlElementWrapper(name="allBooks")//集合外层的节点名
@XmlElement(name="book")
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
@XmlElementWrapper(name="allNames")//数组外层的节点名称
@XmlElement
public String[] getNames() {
return names;
}
public void setNames(String[] names) {
this.names = names;
}
}
2.Book类
package test;
import javax.xml.bind.annotation.XmlAttribute;
public class Book {
private int id;
private String name;
@XmlAttribute
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@XmlAttribute
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3.使用JAXB根据对象生成XML
package test;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class Test {
public static void main(String[] args) {
Student student = new Student();
student.setId(1);
student.setName("zhang");
student.setAge(100);
List<Book> books = new ArrayList<Book>();
Book book1 = new Book();
book1.setId(1);
book1.setName("语文");
Book book2 = new Book();
book2.setId(2);
book2.setName("数学");
books.add(book1);
books.add(book2);
student.setBooks(books);
student.setNames(new String[]{"张三娃子","狗蛋","三娃子"});
try {
File file = new File("D:\\student.xml");//创建xml文件
JAXBContext context = JAXBContext.newInstance(Student.class);
Marshaller mar = context.createMarshaller();
mar.setProperty(Marshaller.JAXB_ENCODING, "gbk");//设置编码
mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);//是否格式化xml文件
mar.setProperty(Marshaller.JAXB_FRAGMENT, false);//是否取消xml头部(<?xml version="1.0" encoding="utf-8" standalone="yes"?>)
mar.marshal(student, file);//把student写入student文件中
mar.marshal(student, System.out); //按xml格式打印student对象
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
4.把xml文件或者String类型的xml字符串解析为对象
package test;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class Test1 {
public static void main(String[] args) {
try {
File file = new File("D:\\student.xml");
//该xml字符串的必须加上头部(<?xml version=\"1.0\" encoding=\"gbk\"?>)才能解析成对象成功
String xml = "<?xml version=\"1.0\" encoding=\"gbk\"?>" +
" <stu id=\"1\">"+
" <age>100</age>"+
" <allBooks>"+
" <book name=\"语文\" id=\"1\"/>"+
" <book name=\"数学\" id=\"2\"/>"+
" </allBooks>"+
" <name>zhang</name>"+
" <allNames>"+
" <names>张三娃子</names>"+
" <names>狗蛋</names>"+
" <names>三娃子</names>"+
" </allNames>"+
" </stu>";
InputStream byteArray = new ByteArrayInputStream(xml.getBytes());//把String字符串转换成流
JAXBContext context = JAXBContext.newInstance(Student.class);
Unmarshaller unMar = context.createUnmarshaller();
//Student student = (Student)unMar.unmarshal(file);//把xml文件直接转换为对象
Student student = (Student)unMar.unmarshal(byteArray);
System.out.println(student.getId());
System.out.println(student.getAge());
System.out.println(student.getName());
for (String s : student.getNames()) {
System.out.println("小名:"+s);
}
for (Book book : student.getBooks()) {
System.out.println("书籍编号:"+book.getId()+"\t书籍名:"+book.getName());
}
} catch (JAXBException e) {
e.printStackTrace();
}
}
}