e20a881b90907a043a8c4940bb0480c778e10340.svn-base 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package com.sinosoft.cm.util;
  2. import java.io.BufferedInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import org.apache.log4j.Logger;
  10. import org.dom4j.Document;
  11. import org.dom4j.DocumentException;
  12. import org.dom4j.DocumentHelper;
  13. import org.dom4j.io.OutputFormat;
  14. import org.dom4j.io.SAXReader;
  15. import org.dom4j.io.XMLWriter;
  16. /**
  17. *
  18. * <p>
  19. * 描述:xml工具类
  20. * </p>
  21. * <p>
  22. * author: liuyuliang
  23. * </p>
  24. * <p>
  25. * filename: XmlUtil.java
  26. * </p>
  27. * <p>
  28. * createDate: 2015年9月25日下午5:55:54
  29. * </p>
  30. *
  31. *
  32. */
  33. public class XmlUtil {
  34. private final Logger log = Logger.getLogger(getClass());
  35. /**
  36. * XML格式化
  37. *
  38. * @param retStr
  39. * @return
  40. * @throws Exception
  41. */
  42. public static String formatXML(String retStr) throws Exception {
  43. String res = null;
  44. Document doc = null;
  45. try {
  46. doc = DocumentHelper.parseText(retStr);
  47. } catch (DocumentException e) {
  48. e.printStackTrace();
  49. return null;
  50. }
  51. // Element root = doc.getRootElement();
  52. // 格式化XML
  53. OutputFormat format = new OutputFormat();
  54. // 设置元素是否有子节点都输出
  55. format.setExpandEmptyElements(true);
  56. // 设置不输出XML声明
  57. format.setSuppressDeclaration(false);
  58. // format.setNewlines(true);
  59. format.setIndent(true);
  60. // format.setEncoding("gbk");
  61. OutputStream outputStream = new ByteArrayOutputStream();
  62. XMLWriter writer = new XMLWriter(outputStream, format);
  63. writer.write(doc);
  64. writer.close();
  65. res = outputStream.toString();
  66. return res;
  67. }
  68. /**
  69. * 字符串转为Document对象
  70. *
  71. * @param xml
  72. * @return
  73. */
  74. public static Document strToXMLDocument(String xml) {
  75. Document document = null;
  76. try {
  77. document = DocumentHelper.parseText(xml);
  78. } catch (DocumentException e) {
  79. e.printStackTrace();
  80. }
  81. return document;
  82. }
  83. /**
  84. * 将document对象转为xml字符串
  85. *
  86. * @param document
  87. * 对象
  88. * @return xml字符串
  89. */
  90. public static String XMLDocumentToStr(Document document) {
  91. return document.asXML();
  92. }
  93. /**
  94. * 通过xml文件名读取到Document对象
  95. *
  96. * @param filename
  97. * @return
  98. */
  99. public static Document getDocument(String filename) {
  100. Document document = null;
  101. try {
  102. document = DocumentHelper.parseText(XmlUtil.getXmlFromFile(filename));
  103. } catch (DocumentException e) {
  104. e.printStackTrace();
  105. }
  106. return document;
  107. }
  108. /**
  109. * 读取xml文件内容
  110. *
  111. * @param path
  112. * 文件路径
  113. * @return
  114. */
  115. private static String getXmlFromFile(String path) {
  116. String retStr = null;
  117. InputStream in;
  118. try {
  119. String s_xmlpath = path;
  120. ClassLoader classLoader = XmlUtil.class.getClassLoader();
  121. in = classLoader.getResourceAsStream(s_xmlpath);
  122. // in = new FileInputStream(path);
  123. BufferedInputStream bis = null;
  124. bis = new BufferedInputStream(in);
  125. byte[] bret;
  126. bret = new byte[bis.available()];
  127. bis.read(bret);
  128. retStr = new String(bret, "UTF-8");
  129. } catch (FileNotFoundException e1) {
  130. e1.printStackTrace();
  131. } catch (IOException e) {
  132. e.printStackTrace();
  133. }
  134. return retStr;
  135. }
  136. /**
  137. * 读取xml文件
  138. *
  139. * @param filePath
  140. * @return document 对象
  141. */
  142. public static Document getDocument2(String filePath) {
  143. File file = new File(filePath);
  144. SAXReader reader = new SAXReader();
  145. Document document = null;
  146. try {
  147. document = reader.read(file);
  148. } catch (DocumentException e) {
  149. e.printStackTrace();
  150. }
  151. return document;
  152. }
  153. }