DOM4J解析XML文件是DOM解析中最好用的一种方法,只要在工程目录下导入dom4j-1.6.1.jar就行了
package cn.moving.parseXml;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
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 Dom4jParse {
@Test
public void read() throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read(new File(
"src/cn/moving/parseXml/book.xml"));
Element books = document.getRootElement();
Element e = (Element) books.elements().get(0);
Element bookName = e.element("书名");
System.out.println(bookName.getText());
Attribute attr = bookName.attribute("name");
System.out.println(attr.getText());
}
@Test
public void add() throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read("src/cn/moving/parseXml/book.xml");
Element book = (Element) document.getRootElement().element("书");
book.addElement("出版社").setText("人民邮电出版社");
OutputFormat format = OutputFormat.createCompactFormat();
// OutputFormat format = new OutputFormat();
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new FileOutputStream(
"src/cn/moving/parseXml/outbook2.xml"));
// XMLWriter writer = new XMLWriter(new
// FileOutputStream("src/cn/moving/parseXml/outbook2.xml"), format);
writer.write(document);
writer.close();
}
@Test
//往指定位置添加元素
public void addAppoint() throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read("src/cn/moving/parseXml/book.xml");
Element book = (Element) document.getRootElement().element("书");
List list = book.elements();//[书名,作者,价格]
Element e = DocumentHelper.createElement("折扣");
e.setText("7折");
list.add(2,e);
OutputFormat format = OutputFormat.createCompactFormat();
// OutputFormat format = new OutputFormat();
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new FileOutputStream(
"src/cn/moving/parseXml/outbook2.xml"));
//漂亮格式输出
// XMLWriter writer = new XMLWriter(new FileOutputStream("src/cn/moving/parseXml/outbook2.xml"), format);
writer.write(document);
writer.close();
}
@Test
// 删除节点<售价>10</售价>
public void delete() throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read("src/cn/moving/parseXml/book.xml");
Element element = document.getRootElement().element("书").element("售价");
element.getParent().remove(element);
XMLWriter writer = new XMLWriter(new FileOutputStream("src/cn/moving/parseXml/book.xml"));
writer.write(document);
writer.close();
}
@Test
// 更新售价
public void update() throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read("src/cn/moving/parseXml/book.xml");
Element element = document.getRootElement().element("书").element("售价");
element.setText("59");
XMLWriter writer = new XMLWriter(new FileOutputStream("src/cn/moving/parseXml/book.xml"));
writer.write(document);
writer.close();
}
@Test
//Powerful Navigation with XPath
public void xpath() throws Exception{
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/cn/moving/parseXml/book.xml"));
String value = document.selectSingleNode("//作者").getText();
Node node = document.selectSingleNode("//书名[@aa='bbb']");
String bookName = document.selectSingleNode("//书名[@aa='bbb']").getText();
String attrValue = node.valueOf("@aa");
System.out.println(value);
System.out.println(attrValue);
System.out.println(bookName);
}
}