javaweb学习总结十一(JAXP对XML文档进行DOM解析)

一:将内存中写好的xml文件读取到硬盘上

javaweb学习总结十一(JAXP对XML文档进行DOM解析)

二:DOM方式对xml文件进行增删改查

1:添加节点(默认是在最后的子节点后面添加)

 @Test
// 向元素中添加节点<version>1.0</version>
public void test5() throws Exception {
Document doc = getDoc();
Node node = doc.getElementsByTagName("book").item(0); // 创建节点
Element newChild = doc.createElement("version");
newChild.setTextContent("1.3");
// 添加节点
node.appendChild(newChild); // 将内存中xml读取到硬盘中
TransformerFactory factory = TransformerFactory.newInstance();
Transformer tf = factory.newTransformer();
tf.transform(new DOMSource(doc), new StreamResult(new FileOutputStream(
"xml/books.xml")));
}

2:在指定位置添加节点

 @Test
// 2:在指定位置添加子节点
public void test6() throws Exception {
Document doc = getDoc();
// 获取要添加节点的父节点
Node node = doc.getElementsByTagName("book").item(1); // 获取参考节点
Node refNode = doc.getElementsByTagName("author").item(1); // 创建节点
Node childNode = doc.createElement("version");
childNode.setTextContent("2.34"); // 将节点添加到指定位置
node.insertBefore(childNode, refNode); // 将内存中xml读取到硬盘中
TransformerFactory factory = TransformerFactory.newInstance();
Transformer tf = factory.newTransformer();
tf.transform(new DOMSource(doc), new StreamResult(new FileOutputStream(
"xml/books.xml")));
}

3:删除节点(方式一)

 @Test
// 3:删除节点
public void test7() throws Exception {
Document doc = getDoc();
// 获取要删除节点的父节点
Node node = doc.getElementsByTagName("book").item(1);
Node childNode = doc.getElementsByTagName("version").item(0);
node.removeChild(childNode); // 将内存中xml读取到硬盘中
TransformerFactory factory = TransformerFactory.newInstance();
Transformer tf = factory.newTransformer();
tf.transform(new DOMSource(doc), new StreamResult(new FileOutputStream(
"xml/books.xml")));
}

4:删除节点(方式二)

 @Test
public void test8() throws Exception {
Document doc = getDoc();
// 获取要删除的节点
Node childNode = doc.getElementsByTagName("version").item(0);
childNode.getParentNode().removeChild(childNode); // 将内存中xml读取到硬盘中
TransformerFactory factory = TransformerFactory.newInstance();
Transformer tf = factory.newTransformer();
tf.transform(new DOMSource(doc), new StreamResult(new FileOutputStream(
"xml/books.xml")));
}

5:修改节点内容

 @Test
// 修改节点内容
public void test9() throws Exception {
Document doc = getDoc();
// 获取要删除的节点
Node node = doc.getElementsByTagName("price").item(0);
node.setTextContent("200"); // 将内存中xml读取到硬盘中
TransformerFactory factory = TransformerFactory.newInstance();
Transformer tf = factory.newTransformer();
tf.transform(new DOMSource(doc), new StreamResult(new FileOutputStream(
"xml/books.xml")));
}

6:添加节点属性

 @Test
// 添加节点属性
public void test10() throws Exception {
Document doc = getDoc();
// 获取要删除的节点
Element ele = (Element) doc.getElementsByTagName("name").item(0);
ele.setAttribute("style", "width:80px;"); // 将内存中xml读取到硬盘中
TransformerFactory factory = TransformerFactory.newInstance();
Transformer tf = factory.newTransformer();
tf.transform(new DOMSource(doc), new StreamResult(new FileOutputStream(
"xml/books.xml")));
}

7:删除节点属性

 @Test
// 删除节点属性
public void test11() throws Exception {
Document doc = getDoc();
// 获取要删除属性的节点
Element ele = (Element) doc.getElementsByTagName("name").item(0);
ele.removeAttribute("style"); // 将内存中xml读取到硬盘中
TransformerFactory factory = TransformerFactory.newInstance();
Transformer tf = factory.newTransformer();
tf.transform(new DOMSource(doc), new StreamResult(new FileOutputStream(
"xml/books.xml")));
}
上一篇:Linux-基础学习(三)-Nginx学习


下一篇:XML文档读取-DOM