package com.bjsxt.cn.jaxp; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; 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; public class Jaxp {
public static void main(String[] args) throws Exception {
//获得解析工厂DocumentBuilderFactory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//获得解析器 DocumentBuilder
DocumentBuilder builder = factory.newDocumentBuilder();
//获得Document对象,得到DOM树
Document document = builder.parse("src/book.xml");
test08(document);
}
// 1、得到某个具体的节点内容:打印第2本书的作者 public static void test01(Document document){
//获得所有的作者标签
NodeList nl = document.getElementsByTagName("作者");
//取得第二个作者的标签
Node node = nl.item(1);
//打印作者 获得标签上的内容
System.out.println(node.getTextContent());
} //2、遍历所有的元素节点:打印元素名称 public static void test02(Node node){
//判断当前节点是否是一个元素节点
short no = node.getNodeType(); //获得当前元素的类型
if(no == Node.ELEMENT_NODE){
//如果是元素节点。就打印
System.out.println(node.getNodeName());
}
//如果不是就获得其子节点
NodeList list = node.getChildNodes();
//然后遍历孩子们递归
for (int i = 0; i < list.getLength(); i++) {
Node n = list.item(i);
test02(n);
}
} //修改节点的文本内容:将作者改为李超 public static void test03(Document document) throws Exception{
//获得所有作者的标签 getElementByName()
NodeList nodeList = document.getElementsByTagName("作者");
//获得第二个作者标签 item()
Node node = nodeList.item(1);
//修改作者标签的setTextContent()
node.setTextContent("李超");
//将内存中的DOM树写入到文本中
//获得一个变压器(写入器)工厂 TransformerFactory()
TransformerFactory factory = TransformerFactory.newInstance();
//获得一个变压器(写入器)Transformer
Transformer transformer = factory.newTransformer();
//写入到硬盘 transformer(new DomSource("源文件"),new StreamReslt("目的地"));
transformer.transform(new DOMSource(document), new StreamResult("src/book.xml"));
} //在第一本书的添加一个子元素 :内部价:99.0 public static void test04(Document document) throws Exception{
//创建一个元素
Element doc = document.createElement("内部价");
//添加元素信息
doc.setTextContent("99.99元");
//找到第一本树的节点
Node node = document.getElementsByTagName("书").item(0);
//他把推送到节点当中
node.appendChild(doc);
//把他刷新出去
//获得一个变压器(写入器)工厂
TransformerFactory factory = TransformerFactory.newInstance();
//获得变压器(写入器)
Transformer transformer = factory.newTransformer();
//写出
transformer.transform(new DOMSource(document), new StreamResult("src/book.xml"));
} //在制定的元素前面添加元素 :在售价后面添加批发价48.8 public static void test05(Document document) throws Exception{
//创建一个元素
Element newEle = document.createElement("批发价");
//给元素添加属性
newEle.setTextContent("48.8元");
//找到要添加后面的元素
Node oldEle = document.getElementsByTagName("售价").item(0);
//加入
oldEle.getParentNode().insertBefore(newEle, oldEle);
//刷新出去
//创建写出工长
TransformerFactory factory = TransformerFactory.newInstance();
//创建写出
Transformer transformer = factory.newTransformer();
//写出
transformer.transform(new DOMSource(document), new StreamResult("src/book.xml"));
} //删除元素:删除内部价 public static void test06(Document document) throws Exception{
//找到元素
Node node = document.getElementsByTagName("内部价").item(0);
//删除元素
node.getParentNode().removeChild(node);
//写出去
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.transform(new DOMSource(document), new StreamResult("src/book.xml"));
} //打印出版社 public static void test07(Document document) throws Exception{
//获得第一本树的节点
Node node = document.getElementsByTagName("书").item(0);
//转化成元素
Element ele = (Element)node;
//获得元素的属性
String str = ele.getAttribute("出版社");
//打印
System.out.println(str);
} //给第二本书添加一个出版社属性 public static void test08(Document document) throws Exception{
//获得第二本书节点
Node node = document.getElementsByTagName("书").item(1);
//转化成元素
Element ele = (Element) node;
//添加属性
ele.setAttribute("出版社", "北京出版社");
//写出
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(new DOMSource(document), new StreamResult("src/book.xml"));
}
} |