package com.xml;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
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.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Test01 {
public static void main(String[] args) throws ParserConfigurationException, IOException, TransformerException, SAXException {
//文件路径及名称
String filepath = "f:" + File.separator + "t.xml" ;
// Test01.domCreateXML(filepath);
Test01.domReadXML(filepath);
}
//dom解析xml文件
public static void domReadXML(String filepath) throws ParserConfigurationException, SAXException, IOException{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File(filepath));
// String root = document.getFirstChild().getNodeName(); //可以拿到根节点
Element element = document.getDocumentElement();
NodeList nodeList = element.getElementsByTagName("Book");
// for (int i = 0; i < nodeList.getLength(); i++) {
// System.out.println(nodeList.item(i).getNodeName() + "--" + nodeList.item(i).getTextContent() );
// }
display(nodeList);
}
// 使用递归遍历个子结点中的子结点
public static void display(NodeList Nlist) {
for (int i = 0; i < Nlist.getLength(); i++) {
Node node = Nlist.item(i);
// System.out.println(node.getTextContent());
if (node.hasChildNodes()) {// 判断该结点是否还有子结点
NodeList list = node.getChildNodes();
display(list); // 调用方法本身
} else {
if (node.getNodeType() == Node.TEXT_NODE) {
System.out.println("---------" + node.getTextContent());
}
break;
}
}
}
//number(5,-2) 最大为:9999900 数字长度为 7位 , 最后两位为00 前面5位为整数 最大为 99999
//dom创建xml文件
public static void domCreateXML(String filepath)throws ParserConfigurationException, IOException, TransformerException{
//文档生成工厂
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//文档生成器
DocumentBuilder db = dbf.newDocumentBuilder();
//创建一个文档
Document doc = db.newDocument();
//创建文档版本
doc.setXmlVersion("1.0");
//创一个文档根节点 名称随意
Element root = doc.createElement("ROOT");
//将根节点放进文档中
doc.appendChild(root);
//创建节点Book
Element book = doc.createElement("Book");
//给book节点添加属性和值
book.setAttribute("id", "jack");
//设置book节点的值
book.setTextContent("dom解析xml文件");
//将book节点放进root根节点
root.appendChild(book);
//拿到文件 没有就创建
File f = new File(filepath);
if(!f.exists()){
f.createNewFile();
}
//xml文件的持有者
StreamResult result = new StreamResult(new FileOutputStream(f));
//dom树资源的持有者
DOMSource domSource = new DOMSource(doc);
//使用工具将文档映射到文件
//拿到转换器工厂
TransformerFactory tff = TransformerFactory.newInstance();
//拿到一个转换器
Transformer tf = tff.newTransformer();
tf.setOutputProperty(OutputKeys.ENCODING, "gbk"); //设置编码
tf.setOutputProperty(OutputKeys.INDENT, "yes"); //格式化输出 添加缩进
//装换的具体实施方法
tf.transform(domSource, result);
}
}