首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

XML与Object 变换

2012-12-18 
XML与Object 转换import java.io.ByteArrayInputStreamimport java.io.ByteArrayOutputStreamimport jav

XML与Object 转换
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

/*
*
* The XML File Reader by Object
*/
public final class XmlReader {

private static String xmlCoding = "UTF-8";

@SuppressWarnings("unchecked")
private static Marshaller createMarshallerByType(Class type) {
Marshaller mar = null;
try {
JAXBContext jax = JAXBContext.newInstance(type);
mar = jax.createMarshaller();
} catch (Exception e) {
e.printStackTrace();
}
return mar;
}

@SuppressWarnings("unchecked")
private static Unmarshaller createUnMarshallerByType(Class type) {
Unmarshaller unMar = null;
try {
JAXBContext jax = JAXBContext.newInstance(type);
unMar = jax.createUnmarshaller();
} catch (Exception e) {
e.printStackTrace();
}
return unMar;
}

/**
* parse XML Object to XML String
*
* @param obj
* @return
*/
public static String parseObjectXmlData(Object obj) {
String backXml = null;
ByteArrayOutputStream baos = null;
try {
baos = new ByteArrayOutputStream();
Marshaller mar = createMarshallerByType(obj.getClass());
mar.marshal(obj, baos);
//backXml = baos.toString();
backXml = new String(baos.toByteArray(), xmlCoding);

} catch (Exception e) {
e.printStackTrace();
} finally {
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

return backXml;
}

/***
* if enCoding is null ,the encoding is UTF-8
*
* @param obj
* @param enCoding
* @return
*/
public static String parseObjectXmlData(Object obj, String enCoding) {
String backXml = null;
ByteArrayOutputStream baos = null;
try {
if (enCoding != null) {
xmlCoding = enCoding;
}
baos = new ByteArrayOutputStream();
Marshaller mar = createMarshallerByType(obj.getClass());
mar.marshal(obj, baos);
backXml = baos.toString();
backXml = new String(baos.toByteArray(), xmlCoding);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return backXml;
}

/**
*
* default xmlCoding is UTF-8
*
* @param type
* @param xmlData
* @return
*/
@SuppressWarnings("unchecked")
public static Object parseXmlDataObject(Class type, String xmlData) {
if (xmlData == null || xmlData.trim().length() == 0) {
return null;
}
ByteArrayInputStream bais = null;
try {
Unmarshaller unMar = createUnMarshallerByType(type);
bais = new ByteArrayInputStream(xmlData.getBytes(xmlCoding));
return unMar.unmarshal(bais);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bais != null) {
try {
bais.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}

/**
* get data from xml
*
* @param type
* @param xmlData
* @param enCoding
* @return
*/
@SuppressWarnings("unchecked")
public static Object parseXmlDataObject(Class type, String xmlData,
String enCoding) {
if (xmlData == null || xmlData.trim().length() == 0) {
return null;
}
ByteArrayInputStream bais = null;
try {
xmlCoding = enCoding;
Unmarshaller unMar = createUnMarshallerByType(type);
bais = new ByteArrayInputStream(xmlData.getBytes(xmlCoding));
return unMar.unmarshal(bais);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bais != null) {
try {
bais.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}

/**
* parse XML Object to XML File
*
* @param obj
* @return
*/
public static boolean parseObjectXmlFile(Object obj) {

FileOutputStream output = null;
try {
String xml = parseObjectXmlData(obj);
output = new FileOutputStream(obj.getClass().getName().concat(".xml"));
output.write(xml.getBytes(xmlCoding));

} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if(output != null){
try {
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

return true;
}

/**
* get data from xml by path
* @param type
* @param xmlPath
* @return
*/
@SuppressWarnings("unchecked")
public static Object parseXmlDataObjectByPath(Class type, String xmlPath) {
if (xmlPath == null || xmlPath.trim().length() == 0) {
return null;
}
InputStream is = null;
try {

is = new FileInputStream(xmlPath);
byte[] b = new byte[is.available()];
is.read(b);
String xmlData = new String(b,xmlCoding);
return parseXmlDataObject(type,xmlData);

} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

return null;
}
}

热点排行