Java中使用jaxp工具以dom方式对xml进行解析
程序员文章站
2022-05-28 14:47:52
...
标题:Java中使用jaxp工具以dom方式对xml进行解析
一、要点
1)获取document对象
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File("src/com/flx/book.xml"));
2)写入文件中
//写入文件中
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
tf.transform(new DOMSource(document),new StreamResult(new File("src/com/flx/book.xml")));
二、实现的操作
1)遍历
- 遍历所有的节点
2)添加 - 测试添加节点【父节点、子节点】
- 向父节点里添加子节点
- 写入xml中
3) 添加到指定位置 - 测试添加到指定位置中【父节点、子节点】
4) 添加属性 - 向指定元素中添加属性【一个节点即可】
5) 删除 - 删除节点【父节点、子节点】
- 删除的方式二【父节点【省】、子节点】
6)更改- 更改【一个节点即可】
完整代码,xml文件如下:
/**
* 测试Jaxp【开发包】对dom【xml解析的一种】的解析
* 使用jaxp工具以dom方式对xml进行解析
* @author dell
*
*/
public class TestJaxpDom {
//测试,获取document,
@Test
public void test01() throws Exception{
//抽象工厂,获取对象document
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File("src/com/flx/book.xml"));
NodeList list = document.getElementsByTagName("书架");
Node node = list.item(0);
System.out.println(node.getTextContent());
System.out.println(node.getNodeName());
}
/**
* 遍历所有的节点
* @throws Exception
*/
@Test
public void test02() throws Exception{
//抽象工厂,获取对象document
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File("src/com/flx/book.xml"));
NodeList list = document.getElementsByTagName("书架");
Node node = list.item(0);//因为书架【根】只有一个,
list(node);
}
//遍历
private void list(Node node) {
//只输出元素
if(node instanceof Element) {
System.out.println(node.getNodeName());
}
NodeList childNodes = node.getChildNodes();
for(int i=0;i<childNodes.getLength();i++) {
Node item = childNodes.item(i);
list(item);
}
}
/**
* 测试添加节点【父节点、子节点】
* 向父节点里添加子节点
* 写入xml中
* @throws Exception
*/
@Test
public void test03() throws Exception{
//抽象工厂,获取对象document
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File("src/com/flx/book.xml"));
//父节点
NodeList list = document.getElementsByTagName("书");
Node node = list.item(0);
//子节点
Element createElement = document.createElement("售价");
createElement.setTextContent("66.6");
//添加到
node.appendChild(createElement);
//写入文件中
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
tf.transform(new DOMSource(document),new StreamResult(new File("src/com/flx/book.xml")));
}
/**
* 测试添加到指定位置中【父节点、子节点】
* @throws Exception
*/
@Test
public void test04() throws Exception{
//抽象工厂,获取对象document
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File("src/com/flx/book.xml"));
//父节点
NodeList list = document.getElementsByTagName("书");
Node node = list.item(1); //找到第一个 书,2
//子节点
Element createElement = document.createElement("售价");
createElement.setTextContent("66.6");
NodeList list2 = document.getElementsByTagName("售价");
Element node2=(Element)list2.item(10); //2,尝试在上下文中引用不存在的节点
//添加到
node.insertBefore(createElement, node2);
//写入文件中
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
tf.transform(new DOMSource(document),new StreamResult(new File("src/com/flx/book.xml")));
}
/**
* 向指定元素中添加属性【一个节点即可】
* @throws Exception
*/
@Test
public void test05() throws Exception{
//抽象工厂,获取对象document
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File("src/com/flx/book.xml"));
//子节点
NodeList list2 = document.getElementsByTagName("售价");
Element node2=(Element)list2.item(4); //2,尝试在上下文中引用不存在的节点
//添加属性
node2.setAttribute("name", "dfadfad");
//写入文件中
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
tf.transform(new DOMSource(document),new StreamResult(new File("src/com/flx/book.xml")));
}
/**
* 删除节点【父节点、子节点】
* @throws Exception
*/
@Test
public void test06() throws Exception{
//抽象工厂,获取对象document
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File("src/com/flx/book.xml"));
//父节点
NodeList list = document.getElementsByTagName("书");
Node node = list.item(1); //找到第一个 书,2
//子节点
NodeList list2 = document.getElementsByTagName("售价");
Element node2=(Element)list2.item(5); //2,尝试在上下文中引用不存在的节点
//删除
node.removeChild(node2);
//写入文件中
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
tf.transform(new DOMSource(document),new StreamResult(new File("src/com/flx/book.xml")));
}
//删除的方式二【父节点【省】、子节点】
@Test
public void test062() throws Exception{
//抽象工厂,获取对象document
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File("src/com/flx/book.xml"));
//子节点
NodeList list2 = document.getElementsByTagName("售价");
Element node2=(Element)list2.item(2); //2,尝试在上下文中引用不存在的节点
//删除
node2.getParentNode().removeChild(node2);
// node2.getParentNode().getParentNode().removeChild(node2.getParentNode());
//写入文件中
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
tf.transform(new DOMSource(document),new StreamResult(new File("src/com/flx/book.xml")));
}
/*
* 更改【一个节点即可】
*/
@Test
public void test07() throws Exception{
//抽象工厂,获取对象document
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File("src/com/flx/book.xml"));
//子节点
NodeList list2 = document.getElementsByTagName("售价");
Element node2=(Element)list2.item(4); //2,尝试在上下文中引用不存在的节点
//更改
node2.setTextContent("6666");
//写入文件中
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
tf.transform(new DOMSource(document),new StreamResult(new File("src/com/flx/book.xml")));
}
}
<?xml version="1.0" encoding="UTF-8" standalone="no"?><书架>
<书>
<书名>javaWeb</书名>
<作者>lkj</作者>
<售价>66.6</售价>
<售价>66.6</售价>
<售价>66.6</售价>
<售价>66.6</售价></书>
<书>
<书名>疯狂Java</书名>
<作者>ggg</作者>
<售价 name="dfadfad">6666</售价>
</书>
<!-- <哈哈哈> </哈哈哈> -->
</书架>
上一篇: 使用DOM对XML文档进行创建和解析
下一篇: Android意图-Intent详解