123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- package com.sinosoft.cm.util;
- import java.io.BufferedInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import org.apache.log4j.Logger;
- import org.dom4j.Document;
- import org.dom4j.DocumentException;
- import org.dom4j.DocumentHelper;
- import org.dom4j.io.OutputFormat;
- import org.dom4j.io.SAXReader;
- import org.dom4j.io.XMLWriter;
- /**
- *
- * <p>
- * 描述:xml工具类
- * </p>
- * <p>
- * author: liuyuliang
- * </p>
- * <p>
- * filename: XmlUtil.java
- * </p>
- * <p>
- * createDate: 2015年9月25日下午5:55:54
- * </p>
- *
- *
- */
- public class XmlUtil {
- private final Logger log = Logger.getLogger(getClass());
- /**
- * XML格式化
- *
- * @param retStr
- * @return
- * @throws Exception
- */
- public static String formatXML(String retStr) throws Exception {
- String res = null;
- Document doc = null;
- try {
- doc = DocumentHelper.parseText(retStr);
- } catch (DocumentException e) {
- e.printStackTrace();
- return null;
- }
- // Element root = doc.getRootElement();
- // 格式化XML
- OutputFormat format = new OutputFormat();
- // 设置元素是否有子节点都输出
- format.setExpandEmptyElements(true);
- // 设置不输出XML声明
- format.setSuppressDeclaration(false);
- // format.setNewlines(true);
- format.setIndent(true);
- // format.setEncoding("gbk");
- OutputStream outputStream = new ByteArrayOutputStream();
- XMLWriter writer = new XMLWriter(outputStream, format);
- writer.write(doc);
- writer.close();
- res = outputStream.toString();
- return res;
- }
- /**
- * 字符串转为Document对象
- *
- * @param xml
- * @return
- */
- public static Document strToXMLDocument(String xml) {
- Document document = null;
- try {
- document = DocumentHelper.parseText(xml);
- } catch (DocumentException e) {
- e.printStackTrace();
- }
- return document;
- }
- /**
- * 将document对象转为xml字符串
- *
- * @param document
- * 对象
- * @return xml字符串
- */
- public static String XMLDocumentToStr(Document document) {
- return document.asXML();
- }
- /**
- * 通过xml文件名读取到Document对象
- *
- * @param filename
- * @return
- */
- public static Document getDocument(String filename) {
- Document document = null;
- try {
- document = DocumentHelper.parseText(XmlUtil.getXmlFromFile(filename));
- } catch (DocumentException e) {
- e.printStackTrace();
- }
- return document;
- }
- /**
- * 读取xml文件内容
- *
- * @param path
- * 文件路径
- * @return
- */
- private static String getXmlFromFile(String path) {
- String retStr = null;
- InputStream in;
- try {
- String s_xmlpath = path;
- ClassLoader classLoader = XmlUtil.class.getClassLoader();
- in = classLoader.getResourceAsStream(s_xmlpath);
- // in = new FileInputStream(path);
- BufferedInputStream bis = null;
- bis = new BufferedInputStream(in);
- byte[] bret;
- bret = new byte[bis.available()];
- bis.read(bret);
- retStr = new String(bret, "UTF-8");
- } catch (FileNotFoundException e1) {
- e1.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return retStr;
- }
- /**
- * 读取xml文件
- *
- * @param filePath
- * @return document 对象
- */
- public static Document getDocument2(String filePath) {
- File file = new File(filePath);
- SAXReader reader = new SAXReader();
- Document document = null;
- try {
- document = reader.read(file);
- } catch (DocumentException e) {
-
- e.printStackTrace();
- }
- return document;
- }
- }
|