DOM4J
DOM4J:开源,支持XML,XPath,XSLT。与JDOM一样应用与JAVA平台。它的API完全支持DOM,SAX,JAXP。http://www.dom4j.org
Dom4j是早期由开发JDOM的人分离出来独立开发的。与JDOM不同的是:Dom4j使用接口和抽象基类,虽然dom4j的API相对复杂一些,但比JDOM更灵活性。SUN的JAXM跟HIBERNATE都是使用DOM4J
Dom4j也使用SAX解析器来分析XML文档,创建DOM4J树。此外,dom4j也接收DOM格式的内容,并提供从dom4j树到SAX事件流或W3C DOM树中的输出机制。
与JDOM不同的是:DOM4J自带了一个SAX解析器Aelfred2,如果没有显示设置,那么默认会加载JAXP配置的解析器,如果创建解析器失败,那么最后将使用dom4j自带的Aelfred2解析器。
DOM4J实例一:DOM4j的API使用
Dom4jTest.java
Java代码
import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class Dom4jTest {
public static void main(String[] args) {
//1.创建DOC对象
Document doc = DocumentHelper.createDocument();
//2.创建处理指令
doc.addProcessingInstruction("xml-stylesheet", "type='text/xsl' href='students.xsl'");
//3.创建根元素
Element root = DocumentHelper.createElement("students");
doc.setRootElement(root);
//4.创建子元素
Element eltStu = root.addElement("student");
Element eltName = eltStu.addElement("name");
Element eltAge = eltStu.addElement("age");
//5.设置文本节点值
eltName.setText("张三");
eltAge.setText("23");
//6.输出
//PrintWriter pw = new PrintWriter(System.out);
OutputFormat outFmt = new OutputFormat(" ",true);
outFmt.setEncoding("gb2312");
try {
//doc.write(pw);
//pw.close();
XMLWriter xmlWriter = new XMLWriter(outFmt);
xmlWriter.write(doc);
} catch (IOException e) {
e.printStackTrace();
}
}
}
DOM4J实例二:遍历整个XML文档树
VisitorTest.java
Java代码
import java.io.File;
import org.dom4j.*;
import org.dom4j.io.SAXReader;
public class VisitorTest{
public static void main(String[] args){
SAXReader saxReader=new SAXReader();
try{
Document doc=saxReader.read(new File("students.xml"));
doc.accept(new MyVisitor());
}catch (DocumentException e){
e.printStackTrace();
}
}
private static class MyVisitor extends VisitorSupport{
public void visit(Attribute node){
System.out.println("Attribute: "+node.getName()+"="+node.getValue());
}
public void visit(Element node){
if(node.isTextOnly()){
System.out.println("Element: "+node.getName()+" "+node.getText());
}else{
System.out.println("------------"+node.getName()+"-------------");
}
}
public void visit(ProcessingInstruction node){
System.out.println("PI: "+node.getTarget()+" "+node.getText());
}
}
}
DOM4J实例三:基于事件的处理(对内容多的XML进行处理)
ElementHandlerTest.java
Java代码
import java.io.File;
import org.dom4j.*;
import org.dom4j.io.SAXReader;
public class ElementHandlerTest{
public static void main(String[] args){
SAXReader saxReader=new SAXReader();
saxReader.addHandler("/students/student",new StudentHandler());
try{
saxReader.read(new File("students.xml"));
}catch (DocumentException e){
e.printStackTrace();
}
}
private static class StudentHandler implements ElementHandler{
public void onStart(ElementPath path){
Element elt=path.getCurrent();
System.out.println("Found student: "+elt.attributeValue("sn"));
path.addHandler("name",new NameHandler());
}
public void onEnd(ElementPath path){
path.removeHandler("name");
}
}
private static class NameHandler implements ElementHandler{
public void onStart(ElementPath path){
System.out.println(path.getPath());
}
public void onEnd(ElementPath path){
Element elt=path.getCurrent();
System.out.println(elt.getName()+": "+elt.getText());
}
}
}