JAXB入门
先来看下JAXB入门资料:http://popomonkey.iteye.com/blog/684285
接下来,直接来看例子:
package javaToXml; import java.util.Set; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; /** * 类Customer.java的实现描述:TODO 类实现描述 * @author sweet 2013-3-23 下午2:12:29 */ @XmlRootElement(name="customer") public class Customer { private String name; private int age; private Set<Book> bookSet; @XmlElement public int getAge() { return age; } public void setAge(int age) { this.age = age; } @XmlElement public String getName() { return name; } public void setName(String name) { this.name = name; } @XmlElementWrapper(name="bookSet") @XmlElement(name="book") public Set<Book> getBookSet() { return bookSet; } public void setBookSet(Set<Book> bookSet) { this.bookSet = bookSet; } @Override public String toString() { return String.format("Customer[age=%d,name=%s,bookSet=%s]", this.age,this.name,this.bookSet); } }
package javaToXml; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlAccessType; /** * 类Book.java的实现描述:TODO 类实现描述 * @author sweet 2013-3-23 下午2:37:22 */ @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement(name="book") public class Book { @XmlElement private int id; @XmlElement private double price; public int getId() { return id; } public void setId(int id) { this.id = id; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return String.format("Book[id=%d,price=%f]", this.id,this.price); } }
下面首先测试java对象序列化为xml:
package javaToXml; import java.io.File; import java.util.HashSet; import java.util.Set; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; /** * 类CustomerTest.java的实现描述:TODO 类实现描述 * @author sweet 2013-3-23 下午2:22:29 */ public class CustomerTest { public static void main(String[] args){ try { Book book1=new Book(); book1.setId(1); book1.setPrice(1.1); Book book2=new Book(); book2.setId(2); book2.setPrice(2.2); Set<Book> bookSet=new HashSet<Book>(); bookSet.add(book1); bookSet.add(book2); Customer customer=new Customer(); customer.setAge(13); customer.setName("sweet"); customer.setBookSet(bookSet); File file=new File("/data/StudySpace/JAXBStudy/xml/customer.xml"); JAXBContext jaxbContext=JAXBContext.newInstance(Customer.class); Marshaller jaxbMarshaller=jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.marshal(customer, file); } catch (JAXBException e) { e.printStackTrace(); } } }
运行下,会生成一个xml文件如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <customer> <age>13</age> <bookSet> <book> <id>1</id> <price>1.1</price> </book> <book> <id>2</id> <price>2.2</price> </book> </bookSet> <name>sweet</name> </customer>
尝试着将该xml反序列化为对象:
package xmlToJava; import java.io.File; import javaToXml.Customer; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; /** * 类CustomerFromXMLTest.java的实现描述:TODO 类实现描述 * @author sweet 2013-3-23 下午3:07:18 */ public class CustomerFromXMLTest { public static void main(String[] args){ try { JAXBContext jaxbContext=JAXBContext.newInstance(Customer.class); Unmarshaller marshaller=jaxbContext.createUnmarshaller(); File file=new File("/data/StudySpace/JAXBStudy/xml/customer.xml"); Customer customer = (Customer)marshaller.unmarshal(file); System.out.println("customer:"+customer); } catch (JAXBException e) { e.printStackTrace(); } } }
看看输出什么:
customer:Customer[age=13,name=sweet,bookSet=[Book[id=1,price=1.100000], Book[id=2,price=2.200000]]]