2.1 使用JAXP 对 xml文档进行DOM解析

时间:2023-03-09 15:35:53
2.1 使用JAXP 对 xml文档进行DOM解析
 //使用 jaxp 对xml文档进行dom解析
public class Demo2 { //必要步骤
@Test
public void test() throws Exception { //1.创建工厂
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); //2.得到dom解析器
DocumentBuilder db = dbf.newDocumentBuilder(); //3.解析XML文档,得到代表文档的Document
Document d = db.parse("src/com/xml/javaweb/book.xml"); }
}

示例:对xml节点进行增删改查

 public class Demo3 {
//读取XML文件中:<书名>javaWEB</书名> 节点中的值
@Test
public void read() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document d = db.parse("src/com/xml/javaweb/book.xml"); NodeList list = d.getElementsByTagName("书名");
Node node = list.item(1);
String text = node.getTextContent();
System.out.println(text);
} //遍历整个XML文档中的节点
@Test
public void read1() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document d = db.parse("src/com/xml/javaweb/book.xml"); //得到根节点
NodeList list = d.getElementsByTagName("书架");
Node root = list.item(0);
list(root);
} private void list(Node root) {
if(root instanceof Element)
System.out.println(root.getNodeName()); NodeList nodelist = root.getChildNodes();
for(int i = 0;i<nodelist.getLength();i++) {
Node node = nodelist.item(i);
list(node);
}
} //拿到 name 属性的值
@Test
public void read2() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document d = db.parse("src/com/xml/javaweb/book.xml"); Element node = (Element) d.getElementsByTagName("书名").item(0); //node 里面的方法不够用了 没有指定名字得到属性,在知道得到的是Element的情况下 ,把node强转一下
String att = node.getAttribute("name");
System.out.println(att);
} //向XML文档中添加节点 <售价>39.00元</售价>
@Test
public void add() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document d = db.parse("src/com/xml/javaweb/book.xml"); //创建节点
Element element = d.createElement("售价");
element.setTextContent("39.00元"); //添加节点
Node node = d.getElementsByTagName("书").item(0);
node.appendChild(element); //更新XML文档
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
tf.transform(new DOMSource(d), new StreamResult(new FileOutputStream("src/com/xml/javaweb/book.xml")));
} //向XML文档中指定位置添加节点 <售价>39.00元</售价>
@Test
public void add2() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document d = db.parse("src/com/xml/javaweb/book.xml"); //创建节点
Element element = d.createElement("售价");
element.setTextContent("29.00元"); //得到参考节点
Element ref = (Element) d.getElementsByTagName("售价").item(0); //添加节点
Node node = d.getElementsByTagName("书").item(0); //往“书”节点的指定位置插入节点
node.insertBefore(element, ref); //在参考节点之前插入新建的节点 //更新XML文档
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
tf.transform(new DOMSource(d), new StreamResult(new FileOutputStream("src/com/xml/javaweb/book.xml")));
} //向XML文档节点上添加属性 <书名> java就业培训课程</书名> 上添加属性 name = "xxxx"
@Test
public void add3() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document d = db.parse("src/com/xml/javaweb/book.xml"); //得到要添加属性的节点
Element element= (Element) d.getElementsByTagName("书名").item(0);
//往指定节点添加属性
element.setAttribute("name", "xxxx"); //更新XML文档
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
tf.transform(new DOMSource(d), new StreamResult(new FileOutputStream("src/com/xml/javaweb/book.xml")));
} //删除XML文档节点 <售价>39.00元</售价>
@Test
public void delete() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document d = db.parse("src/com/xml/javaweb/book.xml"); //得到要删除的节点
Element element= (Element) d.getElementsByTagName("售价").item(1);
//得到要删除的节点的父节点 再调用父节点中删除子节点的方法
element.getParentNode().removeChild(element); //更新XML文档
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
tf.transform(new DOMSource(d), new StreamResult(new FileOutputStream("src/com/xml/javaweb/book.xml")));
} //更新XML文档节点 <作者>张孝祥</作者> -> <作者>xzk</作者>
@Test
public void update() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document d = db.parse("src/com/xml/javaweb/book.xml"); //得到要更新的节点
Element element= (Element) d.getElementsByTagName("作者").item(1);
//更新该节点的文本
element.setTextContent("xzk"); //更新XML文档
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
tf.transform(new DOMSource(d), new StreamResult(new FileOutputStream("src/com/xml/javaweb/book.xml")));
}
}