DOM4J的 DOM解析 和XPATH技术

import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.OutputStreamWriter; import java.util.Iterator; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import org.junit.Test; public class Demo1 { //获取第二个<书名>JavaScript网页开发</书名>的值 @Test public void read() throws Exception{ SAXReader reader = new SAXReader(); Document document = reader.read(new File("src/book.xml")); Element root = document.getRootElement(); Element book = (Element) root.elements("书").get(1); String content = book.element("书名").getText(); System.out.println(content); } //获取第一个<书名 name="xxx">Java就业培训教程</书名> 标签的属性 @Test public void readAttr() throws Exception{ SAXReader reader = new SAXReader(); Document document = reader.read(new File("src/book.xml")); Element root = document.getRootElement(); String value = root.element("书").element("书名").attributeValue("name"); System.out.println(value); } //作业:列出xml文档所有标签 @Test public void list() throws Exception{ SAXReader reader = new SAXReader(); Document document = reader.read(new File("src/book.xml")); Element e = document.getRootElement(); treeWalk(e); } public void treeWalk(Element element) { System.out.println(element.getName()); for ( int i = 0, size = element.nodeCount(); i < size; i++ ) { Node node = element.node(i); if ( node instanceof Element ) { treeWalk( (Element) node ); } } } //向第一本书添加:<售价>59.00元</售价> @Test public void add() throws Exception{ SAXReader reader = new SAXReader(); Document document = reader.read(new File("src/book.xml")); document.getRootElement().element("书").addElement("售价").setText("59.00元"); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); //XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream("src/book.xml"),"UTF-8")); XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),format); writer.write(document); writer.close(); } //向指定位置插入:<售价>59.00元</售价> @Test public void add2() throws Exception{ SAXReader reader = new SAXReader(); Document document = reader.read(new File("src/book.xml")); List list = document.getRootElement().element("书").elements(); Element price = DocumentHelper.createElement("售价"); price.setText("59.00元"); list.add(2, price); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); //XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream("src/book.xml"),"UTF-8")); XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),format); writer.write(document); writer.close(); } @Test public void update() throws Exception{ SAXReader reader = new SAXReader(); Document document = reader.read(new File("src/book.xml")); document.getRootElement().element("书").element("售价").setText("109元"); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); //XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream("src/book.xml"),"UTF-8")); XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),format); writer.write(document); writer.close(); } @Test public void delete() throws Exception{ SAXReader reader = new SAXReader(); Document document = reader.read(new File("src/book.xml")); Element price = document.getRootElement().element("书").element("售价"); price.getParent().remove(price); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); //XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream("src/book.xml"),"UTF-8")); XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),format); writer.write(document); writer.close(); } }  

 

import java.io.File; 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.junit.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Demo2 { //读取:<书名>Java就业培训教程</书名> 标签里面的值 @Test public void read1() throws Exception{ //1.创建解析器工厂 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //2.创建dom解析器 DocumentBuilder builder = factory.newDocumentBuilder(); //3.解析xml文档 Document document = builder.parse(new File("src/book.xml")); NodeList list = document.getElementsByTagName("书名"); Node node = list.item(0); String content = node.getTextContent(); System.out.println(content); } //<书名 name="xxxx">Java就业培训教程</书名> 读取属性值 @Test public void read2() throws Exception{ //1.创建解析器工厂 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //2.创建dom解析器 DocumentBuilder builder = factory.newDocumentBuilder(); //3.解析xml文档 Document document = builder.parse(new File("src/book.xml")); NodeList list = document.getElementsByTagName("书名"); Node node = list.item(0); Element e = (Element) node; String name = e.getAttribute("name"); System.out.println(name); } //递归出xml文档中的所有节点 @Test public void read3() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File("src/book.xml")); Node node = document.getElementsByTagName("书架").item(0); list(node); } public void list(Node node){ if(node instanceof Element){ System.out.println(node.getNodeName()); } NodeList list = node.getChildNodes(); for(int i=0;i<list.getLength();i++){ Node child = list.item(i); list(child); } } //向xml文档中插入:<售价>59.00元</售价> @Test public void insert() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File("src/book.xml")); Node book = document.getElementsByTagName("书").item(0); //创建要添加的节点 Element e = document.createElement("售价"); e.setTextContent("59.00元"); book.appendChild(e); TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer tf = tfactory.newTransformer(); tf.transform(new DOMSource(document), new StreamResult("src/book.xml")); } //向xml文档中指定位置插入:<售价>59.00元</售价> @Test public void insert2() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File("src/book.xml")); Node book = document.getElementsByTagName("书").item(0); //创建要添加的节点 Element e = document.createElement("售价"); e.setTextContent("59.00元"); book.insertBefore(e, document.getElementsByTagName("售价").item(0)); //在此之前插入 TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer tf = tfactory.newTransformer(); tf.transform(new DOMSource(document), new StreamResult("src/book.xml")); } //向xml文档节点插入属性: @Test public void insert3() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File("src/book.xml")); //为书节点添加一个name属性 Node book = document.getElementsByTagName("书").item(0); Element e = (Element)book; e.setAttribute("name", "xxxxxxx"); TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer tf = tfactory.newTransformer(); tf.transform(new DOMSource(document), new StreamResult("src/book.xml")); } //删除节点:<售价>59.00元</售价> @Test public void delete() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File("src/book.xml")); //得到要删除节点的父亲 Node book = document.getElementsByTagName("书").item(0); //得到要删除的节点 Node price = document.getElementsByTagName("售价").item(0); book.removeChild(price); TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer tf = tfactory.newTransformer(); tf.transform(new DOMSource(document), new StreamResult("src/book.xml")); } //删除节点:<售价>59.00元</售价> @Test public void delete1() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File("src/book.xml")); //得到要删除的节点 Node price = document.getElementsByTagName("售价").item(0); //price.getParentNode().removeChild(price); price.getParentNode().getParentNode().getParentNode().removeChild(price.getParentNode().getParentNode()); TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer tf = tfactory.newTransformer(); tf.transform(new DOMSource(document), new StreamResult("src/book.xml")); } //修改节点的值:<作者>张孝祥</作者> 改为 老黎 @Test public void update() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File("src/book.xml")); document.getElementsByTagName("作者").item(0).setTextContent("老黎"); TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer tf = tfactory.newTransformer(); tf.transform(new DOMSource(document), new StreamResult("src/book.xml")); } }  

 

 

 

XPATH:

1

<?xml version="1.0" encoding="UTF-8"?> <users> <user id="1" username="aaa" password="123"/> <user id="2" username="bbb" password="123"/> <user id="3" username="ccc" password="123"/> </users>  

<?xml version="1.0" encoding="UTF-8"?> <书架> <书> <书名 name="xxx">Java就业培训教程</书名> <作者>老黎</作者> <售价>59.00元</售价> <售价>109元</售价> <售价>59.00元</售价> </书> <书> <书名>JavaScript网页开发</书名> <作者>张孝祥</作者> <售价>28.00元</售价> </书> </书架>  

2.

 

 

 

 

 

 

你可能感兴趣的:(xml,exception,list,File,文档,insert)