1.Demo1.java
package cn.itcast.dom4j;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;
public class Demo1 {
// 读取xml文档第二本书的:<书名>Java</书名>
@Test
public void test1() 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 value = book.element("书名").getText();
System.out.println(value);
}
// 读取xml文档第二本书的属性:<书名 name="xx">Java</书名>
@Test
public void test2() 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 value = book.element("书名").attributeValue("name");
System.out.println(value);
}
// 在第一本书上添加一个新的售价:<售价>211元</售价>
@Test
public void addtest() 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(0);
book.addElement("售价").setText("211元");
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("gb2312");
XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),
format);
writer.write(document);
writer.close();
}
// 在第一本书指定位置上添加一个新的售价:<售价>209元</售价>
@Test
public void addtest2() throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/book.xml"));
Element book = (Element) document.getRootElement().element("书");
List list = book.elements();// [书名,作者,售价,售价]
Element price = DocumentHelper.createElement("售价");
price.setText("209元");
list.add(2, price);
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("gb2312");
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 = (Element) document.getRootElement().element("书").element("售价");
price.getParent().remove(price);
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("gb2312");
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"));
Element book = (Element) document.getRootElement().elements("书").get(1);
book.element("作者").setText("hh");
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("gb2312");
XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),
format);
writer.write(document);
writer.close();
}
}
2.Demo2.java
package cn.itcast.dom4j;
import java.io.File;
import java.io.FileOutputStream;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;
public class Demo2 {
/**
* 应用xpath提取xml文档的数据
*
*/
@Test
public void test() throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/book.xml"));
String value = document.selectSingleNode("//作者").getText();
System.out.println(value);
}
}
3.Demo3.java
package cn.itcast.dom4j;
import java.io.File;
import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.junit.Test;
public class Demo3 {
/**
* 查找user.xml文档是否有和用户相匹配的用户名和密码
*
*/
@Test
public void test() throws Exception {
String username = "aaa";
String password = "123";
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/users.xml"));
Node node = document.selectSingleNode("//user[@username='" + username
+ "' and @password='" + password + "']");
if (node == null)
System.out.println("用户名或密码错误!");
else
System.out.println("登录成功!");
}
}