您的位置:首页 > 编程语言 > Java开发

JAVA-实现POJO 到XML的相互转换

2015-06-17 16:11 766 查看
Marshaller 类    ---由POJO 转换成 XML

Unmarshaller 类---由XML转换成POJO

示例

import java.util.Date;

import java.util.Set;

import javax.xml.bind.annotation.XmlAttribute;

import javax.xml.bind.annotation.XmlElement;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement

public class Category {
private String categoryID;
private String categoryName;
private boolean isDeleted;
private Date createDate;
private Set<Product> products;

@XmlAttribute
public String getCategoryID() {
return categoryID;
}
public void setCategoryID(String categoryID) {
this.categoryID = categoryID;
}

@XmlAttribute
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}

@XmlAttribute
public boolean isDeleted() {
return isDeleted;
}
public void setDeleted(boolean isDeleted) {
this.isDeleted = isDeleted;
}

@XmlAttribute
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}

@XmlElement
public Set<Product> getProducts() {
return products;
}
public void setProducts(Set<Product> products) {
this.products = products;
}

public Category() {
super();
}

}

--------------------------------------------------------------------------------------------

import java.util.Date;

import javax.xml.bind.annotation.XmlAttribute;

import javax.xml.bind.annotation.XmlElement;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement

public class Product {
private String productID;
private String productName;
private Double price;
private Date createDate;
private Category  category;

@XmlAttribute
public String getProductID() {
return productID;
}
public void setProductID(String productID) {
this.productID = productID;
}

@XmlAttribute
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}

@XmlAttribute
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}

@XmlAttribute
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}

@XmlElement
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}

public Product(){
super();
}

}

@XmlAttribute
代表的是 属性;@XmlElement 是子元素对象

---------------------------------------------------------

测试方法

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Date;

import java.util.HashSet;

import java.util.Set;

import javax.xml.bind.JAXBContext;

import javax.xml.bind.JAXBException;

import javax.xml.bind.Marshaller;

import javax.xml.bind.Unmarshaller;

import com.proj.urule.entity.Category;

import com.proj.urule.entity.Product;

public class OrderPojoTOXml {

public static void main(String[] args) {
Category category  = initCategory();
//实体类转化成xml
try {
JAXBContext jAXBContext = JAXBContext.newInstance(Category.class);
try {
FileWriter categoryFile = new FileWriter("E:\\TEST\\category.xml");
Marshaller marshaller = jAXBContext.createMarshaller();
marshaller.marshal(category, categoryFile);
} catch (IOException e) {
e.printStackTrace();
}
} catch (JAXBException e) {
e.printStackTrace();
}

//xml转成实体对象
try {
JAXBContext jAXBContextW;
try {
jAXBContextW = JAXBContext.newInstance(Category.class);
FileReader categoryFileRead = new FileReader("E:\\TEST\\category.xml");
Unmarshaller unmarshaller = jAXBContextW.createUnmarshaller();
Category categoryW = (Category) unmarshaller.unmarshal(categoryFileRead);
System.out.println("种类的名称为:"+categoryW.getCategoryName());
} catch (JAXBException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

public static Set<Product> initProducts() {
Set<Product> products = new HashSet<Product>();
Product product_1 = new Product();
product_1.setProductID("p1");
product_1.setProductName("笔记本电脑");
product_1.setPrice(9000.88);
product_1.setCreateDate(new Date());

Product product_2 = new Product();
product_2.setProductID("p2");
product_2.setProductName("手机");
product_2.setPrice(5200.99);
product_2.setCreateDate(new Date());

Product product_3 = new Product();
product_3.setProductID("p3");
product_3.setProductName("PAD");
product_3.setPrice(4200.99);
product_3.setCreateDate(new Date());

products.add(product_1);
products.add(product_2);
products.add(product_3);

return products;
}

public static Category initCategory(){
Category category = new Category();
category.setCategoryID("c1");
category.setCategoryName("电子产品");
category.setDeleted(true);
category.setCreateDate(new Date());
category.setProducts(initProducts());
return category;
}

}

--------------------------------------------------

测试结果

xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<category deleted="true" createDate="2015-06-17T15:27:02.376+08:00"
categoryName="电子产品" categoryID="c1">
<products productName="PAD" productID="p3" price="4200.99"
createDate="2015-06-17T15:27:02.376+08:00" />
<products productName="笔记本电脑" productID="p1" price="9000.88"
createDate="2015-06-17T15:27:02.376+08:00" />
<products productName="手机" productID="p2" price="5200.99"
createDate="2015-06-17T15:27:02.376+08:00" />

</category>

种类的名称为:电子产品
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  JAXB