Java XML Dom解析工具

Java XML Dom解析工具

缩进等

transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

从其他的Document中引入Element

有时会有需求,要从其他的Xml中读取某些节点,添加到当前Xml中,只需要使用 Document.importNode

Document source = XmlUtil.load("1.xml"), target = XmlUtil.getDocumentBuilder().newDocument();
Element node = source.getDocumentElement();
Node imported = target.importNode(node, true);

引入Element时 处理namespace

如果引入节点的命名空间和当前不同,需要处理命名空间问题

XmlUtil.renameNamespaceRecursive(imported, "http://fuckingday.com");

工具类

package org.go;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.Writer; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException; public class XmlUtil { public static DocumentBuilder getDocumentBuilder() {
DocumentBuilder parser = null;
try {
DocumentBuilderFactory df = DocumentBuilderFactory.newInstance();
df.setNamespaceAware(true);
parser = df.newDocumentBuilder();
return parser;
} catch (ParserConfigurationException e) {
e.printStackTrace();
} return null;
} public static Document load(String filePath) throws SAXException, IOException {
DocumentBuilder parser = getDocumentBuilder();
InputStream in = new FileInputStream(filePath);
Document document = parser.parse(in);
return document;
} public static void toFile(Document doc, String file) throws TransformerException {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.transform(new DOMSource(doc), new StreamResult(file));
} public static String toString(Document doc) throws TransformerException {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
Writer writer = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
return writer.toString();
} public static void renameNamespaceRecursive(Node node, String namespace)
{
Document document = node.getOwnerDocument();
if (node.getNodeType() == Node.ELEMENT_NODE)
{
document.renameNode(node, namespace, node.getNodeName());
}
NodeList list = node.getChildNodes();
for (int i = 0; i < list.getLength(); ++i)
{
renameNamespaceRecursive(list.item(i), namespace);
}
} }
上一篇:hrift 的序列化机制


下一篇:【bzoj2820】GCD