一个操作xml的小demo
这个解析是从一个word转化成xml时进行某些节点编辑替换使用的,属于半拉子工程。只有解析和编辑xml的部分。
仅贴一段代码,测试过没问题。但是肯定还有很多情况没考虑到,后边需要逐步完善,目前仅供参考。里边还有很多地方是写死的值,实际使用时还需要再根据自己的实际情况进行修改,欢迎探讨。
package io.melonrind.wxz.oneboot.document.parsexml.demo;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.IOException;
public class ResolverXMLDemo {
public static void main(String[] args) throws Exception{
//这个xml是word转化来的
String path = "C:\\Users\\zwang5\\Desktop\\新建 Microsoft Word 文档\\word\\header1.xml";
// createDocument(new File(path));
// parseDocument(new File(path));
modifyDocument();
}
/**
* 生成xml文件
* @param file
*/
public static void createDocument(File file) {
try {
// 初始化一个XML解析工厂
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// 创建一个DocumentBuilder实例
DocumentBuilder builder = factory.newDocumentBuilder();
// 构建一个Document实例
Document doc = builder.newDocument();
doc.setXmlStandalone(true);
// standalone用来表示该文件是否呼叫其它外部的文件。若值是 ”yes” 表示没有呼叫外部文件
// 创建一个根节点
// 说明: doc.createElement("元素名")、element.setAttribute("属性名","属性值")、element.setTextContent("标签间内容")
Element element = doc.createElement("root");
element.setAttribute("attr", "root");
// 创建根节点第一个子节点
Element elementChildOne = doc.createElement("person");
elementChildOne.setAttribute("attr", "personOne");
element.appendChild(elementChildOne);
// 第一个子节点的第一个子节点
Element childOneOne = doc.createElement("people");
childOneOne.setAttribute("attr", "peopleOne");
childOneOne.setTextContent("attr peopleOne");
elementChildOne.appendChild(childOneOne);
// 第一个子节点的第二个子节点
Element childOneTwo = doc.createElement("people");
childOneTwo.setAttribute("attr", "peopleTwo");
childOneTwo.setTextContent("attr peopleTwo");
elementChildOne.appendChild(childOneTwo);
// 创建根节点第二个子节点
Element elementChildTwo = doc.createElement("person");
elementChildTwo.setAttribute("attr", "personTwo");
element.appendChild(elementChildTwo);
// 第二个子节点的第一个子节点
Element childTwoOne = doc.createElement("people");
childTwoOne.setAttribute("attr", "peopleOne");
childTwoOne.setTextContent("attr peopleOne");
elementChildTwo.appendChild(childTwoOne);
// 第二个子节点的第二个子节点
Element childTwoTwo = doc.createElement("people");
childTwoTwo.setAttribute("attr", "peopleTwo");
childTwoTwo.setTextContent("attr peopleTwo");
elementChildTwo.appendChild(childTwoTwo);
// 添加根节点
doc.appendChild(element);
// 把构造的XML结构,写入到具体的文件中
TransformerFactory formerFactory = TransformerFactory.newInstance();
Transformer transformer = formerFactory.newTransformer();
// 换行
transformer.setOutputProperty(OutputKeys.INDENT, "YES");
// 文档字符编码
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
// 可随意指定文件的后缀,效果一样,但xml比较好解析,比如: E:\\person.txt等
transformer.transform(new DOMSource(doc), new StreamResult(file));
System.out.println("XML CreateDocument success!");
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
/**
* 解析xml并输出解析到的节点信息
* @param file
*/
public static void parseDocument(File file) {
try{
// 初始化一个XML解析工厂
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// 创建一个DocumentBuilder实例
DocumentBuilder builder = factory.newDocumentBuilder();
// 创建一个解析XML的Document实例
Document doc = builder.parse(file);
// 获取根节点名称
String rootName = doc.getDocumentElement().getTagName();
System.out.println("根节点: " + rootName);
System.out.println("递归解析--------------begin------------------");
// 递归解析Element
Element element = doc.getDocumentElement();
parseElement(element);
System.out.println("递归解析--------------end------------------");
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
// 递归方法
public static void parseElement(Element element) {
System.out.print("<" + element.getTagName());
NamedNodeMap attris = element.getAttributes();
for (int i = 0; i < attris.getLength(); i++) {
Attr attr = (Attr) attris.item(i);
System.out.print(" " + attr.getName() + "=\"" + attr.getValue() + "\"");
}
System.out.println(">");
NodeList nodeList = element.getChildNodes();
Node childNode;
for (int temp = 0; temp < nodeList.getLength(); temp++) {
childNode = nodeList.item(temp);
// 判断是否属于节点
if (childNode.getNodeType() == Node.ELEMENT_NODE) {
// 判断是否还有子节点
if(childNode.hasChildNodes()){
parseElement((Element) childNode);
} else if (childNode.getNodeType() != Node.COMMENT_NODE) {
System.out.print(childNode.getTextContent());
}
}
}
System.out.println("</" + element.getTagName() + ">");
}
/**
* 编辑xml中某个节点,目前这个节点是固定的,仅测试用。
* 你自己用的时候需要再修改,如果不知道要修改哪儿可以再问
* @throws Exception
*/
public static void modifyDocument() throws Exception{
String path = "C:\\Users\\zwang5\\Desktop\\新建 Microsoft Word 文档\\word\\header1.xml";
//xml读入
File file = new File(path);
//初始化工厂
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//创建实例
DocumentBuilder builder = factory.newDocumentBuilder();
//解析xml
Document doc = builder.parse(file);
//按名称获取节点
Node root = doc.getElementsByTagName("w:hdr").item(0);
//递归获取节点并替换内容
getNodeByName(root,"v:textpath");
// 创建 TransformerFactory 对象
TransformerFactory transformerFactory = TransformerFactory.newInstance();
// 创建 Transformer 对象
Transformer transformer = transformerFactory.newTransformer();
//创建 DOMSource 对象
DOMSource domSource = new DOMSource(doc);
//创建 StreamResult 对象
StreamResult reStreamResult = new StreamResult(file);
transformer.transform(domSource, reStreamResult);
// 输出测试结果
StreamResult consoleResult = new StreamResult(System.out);
transformer.transform(domSource, consoleResult);
}
/**
* 根据节点名称获取节点内容并替换
* 目前获取的节点是测试用的死值,替换的内容也是死值
* 你自己使用需要修改成自己需要的
* @param node
* @param nodeName
* @throws Exception
*/
public static void getNodeByName(Node node,String nodeName) throws Exception{
NodeList nodeList = node.getChildNodes();
Node childNode;
for (int temp = 0; temp < nodeList.getLength(); temp++) {
childNode = nodeList.item(temp);
if(nodeName.equalsIgnoreCase(childNode.getNodeName())){
System.out.print("获取到要替换的节点");
//获取节点所有属性
NamedNodeMap namedNodeMap = childNode.getAttributes();
//获取某一属性
Node strings = namedNodeMap.getNamedItem("string");
if(strings == null){
continue;
}
//设置属性值
strings.setTextContent("测试替换水印");
break;
}
// 判断是否属于节点
if (childNode.getNodeType() == Node.ELEMENT_NODE) {
// 判断是否还有子节点
if(childNode.hasChildNodes()){
getNodeByName(childNode,nodeName);
} else if (childNode.getNodeType() != Node.COMMENT_NODE) {
System.out.print(childNode.getTextContent());
}
}
}
}
}