1.关于Dom4j.
Dom4j是一个简单、灵活的开放源代码库。Dom4j是由早期开发JDOM的人分离出来而后独立开发的,与JDOM不同的是,dom4j使用接口和抽象基类,虽然Dom4j的API相对要复杂一些,但它提供了比JDOM更好的灵活性。
Dom4j是一个非常优秀的Java XML API,具有性能优异、功能强大和极易使用的特点。现在很多软件采用Dom4j,例如Hibernate,包括sun公司自己的JAXM也用了Dom4j.
使用Dom4j开发,需下载dom4j相应的jar文件.
Dom4j官方网站: http://www.dom4j.org/
Dom4j下载: http://nchc.dl.sourceforge.net/sourceforge/dom4j/dom4j-1.6.1.zip
2.Dom4j操作xml文档案例
import org.dom4j.io.SAXReader;
public class Demo1{
//读取xml案例
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 value = book.element("书名").getText();
//String value = book.element("书名").attribute("name").getValue(); //属性值
//String value = book.element("书名").attributeValue("name"); //属性值
System.out.println(value);
}
//添加xml标签.
public void add1() throws Exception{
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/book.xml"));
Element book = document.getRootElement().element("书");
book.addElement("售价").setText("200元");
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),format));
writer.write(document);
writer.close();
}
//指定位置添加xml标签.
public void add2() throws Exception{
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/book.xml"));
Element book = document.getRootElement().element("书");
List list = book.elements();
Element price = DocumentHelper.createElement("售价");
price.setText("309元");
list.add(2, price);
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),format));
writer.write(document);
writer.close();
}
//删除xml标签.
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 FileOutputStream("src/book.xml"),format));
writer.write(document);
writer.close();
}
//更改xml标签.
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("某某");
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),format));
writer.write(document);
writer.close();
}
}
3.用XPath提取xml数据
案例1:
import java.io.File;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
public class demo{
public static void main(String[] args) throws DocumentException{
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/book.xml"));
String value = document.selectSingleNode("//作者").getText();
System.out.println(value);
}
}
案例2:
import java.io.File;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
public class demo{
public static void main(String[] args) throws DocumentException{
String username = "ggg";
String password = "123";
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/user.xml"));
Node node = document.selectSingleNode("//user[@username='"+username+"' and @password='"+password+"']");
if (node==null){
System.out.println("用户名或密码错误");
}else{
System.out.println("登录成功");
}
}
}