package com.zj.docconvert.util;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
//
//
// @Description: javaBean、xml之间相互转化
// @author Jian Jang
// @version 1.0 2016年5月20日
///
public class JavaxObject2XmlUtil {
@SuppressWarnings("unchecked")
private T unmarshal(Class clazz, InputStream is) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(clazz);
Unmarshaller un = context.createUnmarshaller();
return (T) un.unmarshal(is);
}
//
// 将xml文件转化成指定的javaBean
// @param clazz javaBean
// @param file xml文件
// @return
// @throws JAXBException
// @throws FileNotFoundException
///
public T unmarshal(Class clazz, File file) throws JAXBException, FileNotFoundException {
return unmarshal(clazz, new FileInputStream(file));
}
//
// 将javaBean转化成xml格式
// @param element javaBean
// @param charset 编码
// @return 返回转化后的xml字符串
///
public String marshal(T element, String charset) {
JAXBContext jc;
Marshaller m;
String xmlString = null;
try {
jc = JAXBContext.newInstance(element.getClass());
m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_ENCODING, charset);
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
m.marshal(element, bos);
xmlString = bos.toString(charset);
return xmlString;
} catch (JAXBException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return xmlString;
}
//
// 将javaBean转化成xml格式文件并保存到指定的文件
// @param element javaBean
// @param charset 编码
// @param filePath 文件名称(包括路径)
///
public void marshal(T element, String charset, String filePath) {
JAXBContext jc = null;
Marshaller m = null;
String xmlString = null;
FileOutputStream fos = null;
try {
jc = JAXBContext.newInstance(element.getClass());
m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_ENCODING, charset);
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
m.marshal(element, bos);
xmlString = bos.toString(charset);
// 写入指定的文件
fos = new FileOutputStream(filePath);
fos.write(xmlString.getBytes(charset));
fos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
二、示例
实体对象为部门和员工(一对多的关系)
//1、部门实体类
package com.zj.util.test;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
//
// @Description: 部门实体类
// @author Jian Jang
// @version 1.0 2016年5月20日
///
@XmlRootElement
public class Department {
private int id ;
private String dName;
private List employee;
public void setEmployee(List employee) {
this.employee = employee;
}
public List getEmployee() {
return employee;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setdName(String dName) {
this.dName = dName;
}
public String getdName() {
return dName;
}
}
//2、员工实体类
package com.zj.util.test;
//
// @Description: 员工实体类
// @author Jian Jang
// @version 1.0 2016年5月20日
///
public class Employee {
private int sign;
private String name;
public int getSign() {
return sign;
}
public void setSign(int sign) {
this.sign = sign;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//3、测试类
@Test
public void test02(){
Department d = new Department();
d.setId(1);
d.setdName("技术中心");
List employees = new ArrayList();
Employee e1= new Employee();
e1.setSign(20160001);
e1.setName("张一山");
Employee e2= new Employee();
e2.setSign(20160002);
e2.setName("李思敏");
Employee e3= new Employee();
e3.setSign(20160003);
e3.setName("王妩媚");
employees.add(e1);
employees.add(e2);
employees.add(e3);
d.setEmployee(employees);
JavaxObject2XmlUtil jax = new JavaxObject2XmlUtil();
try {
System.out.println(jax.marshal(d, "UTF-8"));
jax.marshal(d, "UTF-8", "d:/test/test.xml");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//4、输出