xml与对象转换

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

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")
	public 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) {
			//ResourceUtils.printLog(e.toString());
		} finally {
			if (bais != null) {
				try {
					bais.close();
				} catch (IOException e) {
					//ResourceUtils.printLog(e.toString());
				}
			}
		}
		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,String fullpath) {

		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(fullpath);
			Marshaller mar = createMarshallerByType(obj.getClass());
			mar.marshal(obj, fos);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return true;
	}
	
	/**
	 * get data from xml by file
	 * @param type
	 * @param xmlPath
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Object parseXmlDataObjectByPath(Class type, File file) {
		if (file == null) {
			return null;
		}
		try {
			Unmarshaller unMar = createUnMarshallerByType(type);
			return unMar.unmarshal(file);
		} catch (Exception e) {
			e.printStackTrace();
		} 
		
		return null;
	}
}

你可能感兴趣的:(xml)