Dom4j是一个非常优秀的Java XML API,具有性能优异、功能强大和极易使用的特点。现在很多软件采用的Dom4j,例如Hibernate,包括sun公司自己的JAXM也用了Dom4j。
使用Dom4j开发,需下载dom4j相应的jar文件。
开放源代码使用技巧:docs->index.html->quick start
<span style="white-space:pre"> </span>SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/book1.xml"));
Element root = document.getRootElement();
查询标签:
@Test
public void read() throws DocumentException {
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/book1.xml"));
Element root = document.getRootElement();
List list = root.elements("书");
Element book = (Element)list.get(1);
Element book_name = book.element("书名");
System.out.println(book_name.getText());
}
查询属性:
// <书 name="yyyyyyy">
@Test
public void readAttribute() throws DocumentException {
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/book1.xml"));
Element root = document.getRootElement();
String value = root.element("书").attributeValue("name");
System.out.println(value);
}
增加节点:
// 增加售价节点
@Test
public void add() throws DocumentException, IOException {
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/book1.xml"));
Element price = DocumentHelper.createElement("售价");
price.setText("19元");
document.getRootElement().element("书").add(price);
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(new FileOutputStream("src/book1.xml"),format);
writer.write(document);
writer.close();
}
// 修改: <售价>109</售价>
@Test
public void update() throws IOException, DocumentException {
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/book1.xml"));
Element price = (Element) document.getRootElement().element("书").elements("售价").get(1);
price.setText("209");
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(new FileOutputStream("src/book1.xml"),format);
writer.write(document);
writer.close();
}
删除:
// 删除节点<售价>109</售价>
@Test
public void delete() throws DocumentException, IOException {
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/book1.xml"));
Element price = (Element) document.getRootElement().element("书").elements("售价").get(0);
price.getParent().remove(price);
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(new FileOutputStream("src/book1.xml"),format);
writer.write(document);
writer.close();
}
指定节点的插入:
// 向指定位置增加售价节点
@Test
public void add2() throws DocumentException, IOException {
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/book1.xml"));
Element price = DocumentHelper.createElement("售价");
price.setText("19元");
List list = document.getRootElement().element("书").elements();
list.add(1,price);
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(new FileOutputStream("src/book1.xml"),format);
writer.write(document);
writer.close();
}
xPath的使用:
查找某一节点:
@Test
public void findWithXpath() throws DocumentException {
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/book1.xml"));
Element name = (Element) document.selectNodes("//书名").get(1);
System.out.println(name.getText());
}
@Test
public void findUsers() throws DocumentException, IOException {
/* BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入用户名:");
String username = br.readLine();
System.out.print("请输入密码:");
String password = br.readLine();*/
String username = "aaa";
String password = "123";
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/users.xml"));
Element e = (Element) document.selectSingleNode("//user[@username='" + username + "' and @password='" + password + "']");
if (e != null){
System.out.println("登陆成功!");
}else{
System.out.println("用户不存在或密码错误!");
}