|
| 1 | +package iqq.app.util; |
| 2 | + |
| 3 | +import org.dom4j.Document; |
| 4 | +import org.dom4j.DocumentException; |
| 5 | +import org.dom4j.Element; |
| 6 | +import org.dom4j.Node; |
| 7 | +import org.dom4j.io.OutputFormat; |
| 8 | +import org.dom4j.io.SAXReader; |
| 9 | +import org.dom4j.io.XMLWriter; |
| 10 | + |
| 11 | +import java.io.File; |
| 12 | +import java.io.FileOutputStream; |
| 13 | +import java.io.IOException; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.Map; |
| 16 | + |
| 17 | +/** |
| 18 | + * XML操作类 读取/写入/获取值 |
| 19 | + * |
| 20 | + * Project : iqq |
| 21 | + * Author : 承∮诺 < [email protected] > |
| 22 | + * Created : 14-4-16 |
| 23 | + * License : Apache License 2.0 |
| 24 | + */ |
| 25 | +public class XmlUtils { |
| 26 | + private static Map<String, Document> xmlCache = new HashMap<String, Document>(); |
| 27 | + |
| 28 | + /** |
| 29 | + * 获取XML节点的值 |
| 30 | + * |
| 31 | + * @param filename |
| 32 | + * @param key |
| 33 | + * @return |
| 34 | + * @throws DocumentException |
| 35 | + */ |
| 36 | + public static String getNodeText(String filename, String key) throws DocumentException { |
| 37 | + return getNode(filename, key).getText(); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * 获取XML节点 |
| 42 | + * |
| 43 | + * @param filename |
| 44 | + * @param key |
| 45 | + * @return |
| 46 | + * @throws DocumentException |
| 47 | + */ |
| 48 | + public static Node getNode(String filename, String key) throws DocumentException { |
| 49 | + return getElement(filename).selectSingleNode(key); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * 获取XML元素 |
| 54 | + * |
| 55 | + * @param filename |
| 56 | + * @return |
| 57 | + * @throws DocumentException |
| 58 | + */ |
| 59 | + public static Element getElement(String filename) throws DocumentException { |
| 60 | + return readXml(filename).getRootElement(); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * 读入XML文件 |
| 65 | + * |
| 66 | + * @param filename |
| 67 | + * @return |
| 68 | + * @throws DocumentException |
| 69 | + */ |
| 70 | + public static Document readXml(String filename) throws DocumentException { |
| 71 | + Document document = null; |
| 72 | + if(xmlCache.get(filename) == null) { |
| 73 | + // 创建SAXReader读取器 |
| 74 | + SAXReader saxReader = new SAXReader(); |
| 75 | + // 读取xml |
| 76 | + document = saxReader.read(new File(filename)); |
| 77 | + // 放到缓存中 |
| 78 | + xmlCache.put(filename, document); |
| 79 | + } |
| 80 | + return document; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * 写入XML文件 |
| 85 | + * |
| 86 | + * @param document |
| 87 | + * @param xmlFile |
| 88 | + * @throws IOException |
| 89 | + */ |
| 90 | + public static void writeXml(Document document, String filename) throws IOException { |
| 91 | + OutputFormat outputFormat = OutputFormat.createPrettyPrint();// 设置XML文档输出格式 |
| 92 | + outputFormat.setEncoding("UTF-8");// 设置XML文档的编码类型 |
| 93 | + outputFormat.setIndent(true);// 设置是否缩进 |
| 94 | + outputFormat.setIndent(" ");// 以TAB方式实现缩进 |
| 95 | + outputFormat.setNewlines(true);// 设置是否换行 |
| 96 | + XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(filename), outputFormat); |
| 97 | + xmlWriter.write(document); |
| 98 | + xmlWriter.close(); |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments