导入dom4j开发包:dom4j-1.6.1.jar
- 在工程下建一个文件夹lib,将dom4j-1.6.1.jar拷到里边。右键add to build path。
- dom4j-1.6.1\lib文件夹下还有一些jar包,是开发过程中dom4j所需要依赖的jar包,如开发过程中报错,则需导入。
用dom4j怎么做呢?
只要是开源jar包提供给你的时候,它会在开源包里面提供相对应的示例程序。入门文档里会清楚的告诉你第一步、第二步、第三步.........
dom4j-1.6.1文件夹里的docs文件夹就是说明文档。双击index.html会到达入门文档的首页。
点击Quick start快速入门。
DOM4j中,获得Document对象的方式有三种:
1. 读取XML文件,获得document对象
SAXReader reader= new SAXReader();
Document document = reader.read(new File("input.xml"));
2. 解析XML形式的文本,得到document对象.
String text ="<members></members>";
Document document = DocumentHelper.parseText(text);
3. 主动创建document对象.
Documentdocument = DocumentHelper.createDocument();
//创建根节点
Element root = document.addElement("members");
dom4j想获取某一节点,只能一级一级往下获取。
package cn.mengmei.dom4j;
import java.io.FileOutputStream;
import java.io.IOException;
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 {
@Test
public void read() throws DocumentException{
SAXReader reader = new SAXReader();
Document document = reader.read("src/book.xml");
Element root = document.getRootElement();
System.out.println(root.element("书").element("书名").getText());
}
@Test
public void readAttr() throws DocumentException{
SAXReader reader = new SAXReader();
Document document = reader.read("src/book.xml");
Element root = document.getRootElement();
System.out.println(root.element("书").attributeValue("name"));
}
@Test
public void add() throws DocumentException, IOException{
SAXReader reader = new SAXReader();
Document document = reader.read("src/book.xml");
Element root = document.getRootElement();
/*Element newNode = DocumentHelper.createElement("售价");
newNode.setText("19元");
root.element("书").add(newNode);*/
root.element("书").addElement("售价").addText("20元");
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),format);
writer.write(document);
writer.close();
}
@Test
public void update() throws DocumentException, IOException{
SAXReader reader = new SAXReader();
Document document = reader.read("src/book.xml");
Element root = document.getRootElement();
//root.element("书").element("售价").setText("50");
/*Element ele = (Element)root.element("书").elements("售价").get(2);
ele.setText("687");*/
root.element("书").setAttributeValue("name", "yyyYYY");
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),format);
writer.write(document);
writer.close();
}
@Test
public void delete() throws DocumentException, IOException{
SAXReader reader = new SAXReader();
Document document = reader.read("src/book.xml");
Element root = document.getRootElement();
Element ele = (Element)root.element("书").elements("售价").get(1);
ele.getParent().remove(ele);
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),format);
writer.write(document);
writer.close();
}
//指定位置插元素
@Test
public void add2APoint() throws DocumentException, IOException{
SAXReader reader = new SAXReader();
Document document = reader.read("src/book.xml");
Element root = document.getRootElement();
Element newNode = DocumentHelper.createElement("售价");
newNode.setText("999");
List<Element> list = root.element("书").elements();
list.add(1, newNode);
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),format);
writer.write(document);
writer.close();
}
@Test
public void findwithXpath() throws DocumentException{
SAXReader reader = new SAXReader();
Document document = reader.read("src/book.xml");
//获取第二个书名的值
Element ele = (Element)document.selectNodes("//书名").get(1);
System.out.println(ele.getText());
}
@Test
public void login() throws DocumentException{
SAXReader reader = new SAXReader();
Document document = reader.read("src/users.xml");
String username = "lisi";
String password = "123";
Node node = document.selectSingleNode("//user[@username='"+username+"' and @password='"+password+"']");
if(node != null){
System.out.println("登录成功!");
}else{
System.out.println("用户名或密码错误......");
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<书架>
<书 name="yyyYYY">
<书名>Java就业培训教程</书名>
<售价>999</售价>
<售价>999</售价>
<作者>张孝祥</作者>
<售价>20元</售价>
<售价>20元</售价>
</书>
<书>
<书名>JavaScript网页开发</书名>
<作者>张孝祥</作者>
<售价>28.00元</售价>
</书>
</书架>