package jaxb.shop;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
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;
public class JAXB
{
/**
* 把对象转换成xml
* @param object 转换的对象
* @param charset xml的字符集
* @param format 是否格式化
* @return
* @throws JAXBException
*/
public static String objectToXml(Object object, String charset, boolean format)
throws JAXBException
{
ByteArrayOutputStream baos = null;
try {
Marshaller m = JAXBContext.newInstance(new Class[] { object.getClass() }).createMarshaller();
//指定生成xml的编码
m.setProperty("jaxb.encoding", charset);
//用来指定是否使用换行和缩排对已编组 xml 数据进行格式化的属性名称
m.setProperty("jaxb.formatted.output", Boolean.valueOf(format));
baos = new ByteArrayOutputStream();
m.marshal(object, baos);
return baos.toString();
}
finally {
if (baos != null)
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 对象转换为字符串
* @param object 转换对象
* @param format 是否格式化
* @return
* @throws JAXBException
*/
public static String objectToXml(Object object, boolean format)
throws JAXBException
{
ByteArrayOutputStream baos = null;
try {
Marshaller m = JAXBContext.newInstance(new Class[] { object.getClass() }).createMarshaller();
m.setProperty("jaxb.encoding", System.getProperty("file.encoding"));
m.setProperty("jaxb.formatted.output", Boolean.valueOf(format));
baos = new ByteArrayOutputStream();
m.marshal(object, baos);
return baos.toString();
}
finally {
if (baos != null)
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* xml转换成对象
* @param objectClass 转换成对象Class
* @param xml xml字符串
* @param charset 指定字符集
* @return
* @throws JAXBException
*/
public static Object xmlToObject(Class objectClass, String xml, String charset)
throws JAXBException
{
InputStream is = null;
Unmarshaller um = null;
try {
is = new ByteArrayInputStream(xml.getBytes(charset));
um = JAXBContext.newInstance(new Class[] { objectClass }).createUnmarshaller();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
if (is != null)
try {
is.close();
} catch (IOException exx) {
exx.printStackTrace();
}
}
finally
{
if (is != null)
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return um.unmarshal(is);
}
/**
* xml转换成对象
* @param objectClass
* @param xml
* @return
* @throws JAXBException
*/
public static Object xmlToObject(Class objectClass, String xml)
throws JAXBException
{
InputStream is = null;
Unmarshaller um = null;
try {
is = new ByteArrayInputStream(xml.getBytes(System.getProperty("file.encoding")));
um = JAXBContext.newInstance(new Class[] { objectClass }).createUnmarshaller();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
if (is != null)
try {
is.close();
} catch (IOException exx) {
exx.printStackTrace();
}
}
finally
{
if (is != null)
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return um.unmarshal(is);
}
}