com.rt.core.util.XMLUtil Maven / Gradle / Ivy
package com.rt.core.util;
import com.rt.core.constant.RTConst;
import com.rt.core.log.Log;
import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import javax.xml.transform.TransformerException;
import java.io.CharArrayWriter;
import java.io.InputStream;
import java.net.URL;
import java.util.List;
import java.util.Map;
/**
* xml(dom4j)基础操作
* 以log4j包为基础,提供对xml文件的创建/修改/删除操作.
*
* @version V1.02
*/
public class XMLUtil {
private static Log log = Log.getLog(XMLUtil.class);
/**
* 构造xml对象
*
* @param str str
* @return doc org.dom4j.Document
* @throws DocumentException DocumentException
*/
public static Document buildDocument(String str) throws DocumentException {
return DocumentHelper.parseText(str);
}
/**
* 读取xml文件
*
* @param inputStream inputStream
* @return doc org.dom4j.Document
* @throws DocumentException DocumentException
*/
public static Document readDocument(InputStream inputStream)
throws DocumentException {
return new SAXReader().read(inputStream);
}
/**
* 读取xml文件
*
* @param path path
* @return doc org.dom4j.Document
* @throws DocumentException DocumentException
*/
public static Document readDocument(String path) throws Exception {
SAXReader saxReader = new SAXReader();
return saxReader.read(path);
}
/**
* 美化格式输出
*
* @param doc doc
* @return String
*/
public static String prettyFormat(Document doc) {
return format(doc, OutputFormat.createPrettyPrint());
}
/**
* 紧凑格式输出
*
* @param doc doc
* @return String
*/
public static String compactFormat(Document doc) {
return format(doc, OutputFormat.createCompactFormat());
}
/**
* 是否美化格式
*
* @param doc doc
* @param prettyFormat prettyFormat
* @return Document
*/
public static String format(Document doc, boolean prettyFormat) {
return prettyFormat ? prettyFormat(doc) : compactFormat(doc);
}
/**
* 格式化xml文档.
*
* @param doc doc
* @param outputFormat outputFormat
* @return String
*/
public static String format(Document doc, OutputFormat outputFormat) {
XMLWriter writer = null;
CharArrayWriter charOut = new CharArrayWriter();
try {
outputFormat.setEncoding(RTConst.UTF_8);
writer = new XMLWriter(charOut, outputFormat);
writer.write(doc);
writer.close();
} catch (Exception e) {
log.error(e.toString());
} finally {
charOut.close();
}
return charOut.toString();
}
/**
* 通过XPath查找文档中的元素
*
* @param node Branch(Document or Element)
* @param xPath xPath
* @return List
*/
public static List selectElement(Branch node, String xPath) {
return node.selectNodes(xPath);
}
/**
* 通过XPath查找文档中的元素
*
* @param node Branch(Document or Element)
* @param xPath xPath
* @return Element
*/
public static Element selectSingleElement(Branch node, String xPath) {
return (Element) node.selectSingleNode(xPath);
}
/**
* 通过XPath删除文档中的元素
*
* @param node Branch(Document or Element)
* @param xPath xPath
*/
public static void removeElement(Branch node, String xPath) {
List n = node.selectNodes(xPath);
if (n.size() > 0) {
Element parent = ((Element) n.get(0)).getParent();
if (parent == null) {
return;
}
for (Object aN : n) {
parent.remove((Element) aN);
}
}
}
/**
* 添加xml头
*
* @param doc doc
* @param name name
* @param map map
*/
public static void processingInstruction(Document doc, String name, Map map) {
doc.processingInstruction(name).setValues(map);
}
/**
* 读取xml文件
*
* @param url url
* @return Document
*/
public static Document readDocument(URL url) {
try {
return new SAXReader().read(url);
} catch (DocumentException e) {
log.error(e.toString());
return null;
}
}
/**
* 创建带有根节点的XML对象
*
* @param name name
* @return Document
*/
public static Document createDocument(String name) {
Document doc = DocumentHelper.createDocument();
doc.addElement(name);
return doc;
}
/**
* 创建空节点.
*
* @return Document
*/
public static Document createDocument() {
return DocumentHelper.createDocument();
}
/**
* 创建一个 Element
*
* @param name name
* @return Document
*/
public static Element createElement(String name) {
Document doc = DocumentHelper.createDocument();
return doc.addElement(name);
}
/**
* 调用xsl转换xml
*
* @param xslPath xslPath
* @param xml xml
* @return String xsl+xml
* @throws DocumentException DocumentException
* @throws TransformerException TransformerException
* @see XSLUtil#transformer(URL, String)
*/
public static String xslTransformer(URL xslPath, String xml)
throws DocumentException, TransformerException {
return XSLUtil.transformer(xslPath, xml);
}
}