Dom4j解析和创建XML文档

来源:互联网 发布:手机概率分析软件 编辑:程序博客网 时间:2024/06/05 16:37

dom4j是一套开源的XML解析工具,完全支持DOM,SAX和JAXP机制,与传统的Xerces-J相比,dom4j更为简单易用,因此实际项目中往往采用它作为xml解析器。


历史:xml4j是由IBM开发的, 它后来把代码捐赠给了Apache, 成为XercesJ-1,后来Apache对Xerces做了全面改写,成为现在的XercesJ-2。同时Apache还受Sun的委托,开发另一个解析器 Crimson ,JDK1.4 自带了JAXP框架,它的默认解析器实现就是Crimson。jdom是xml parser的java实现,封装了一些复杂的sax2和dom的API,简化了操作。一些原来做jdom的人因为对jdom的设计和实现有异意,脱离了jdom开发了dom4j。

 

dom4j只提供了java版本的,往往有很多的开源项目都使用它作为解析器比如hibernate框架。

 

dom4j支持多种解析机制:

 

DOMReader :它负责根据W3C的DOM树创建dom4j树。

SAXReader:它基于SAX解析机制来解析一份XML文档,并将其转换为dom4j树。

XPP3Reader:其底层需要依赖于XML Pull Parser 3.x来解析XML文档,并将其装换为dom4j树。

XPPReader :他是基于XML pull Parser 2.x的解析器,目前不支持注解,CDATA和处理指令。

 

 

使用dom4j解析xml文档 (代码来源《疯狂xml讲义》)

使用它来解析xml文档非常简单,在将xml文档转换成dom4j树之后,程序就可使用一致的编程模型来处理xml文档。

这里要注意的是导入包的时候一定不能出错,因为在java自己的jdk中也有


package com.gengu;import java.io.File;import java.util.List;import org.dom4j.io.SAXReader;import org.dom4j.Document;import org.dom4j.Element;import org.dom4j.Attribute;public class test1 {public static void main(String[] args) throws Exception{//创建启用DTD验证功能的SAXReader对象SAXReader reader = new SAXReader();Document document = reader.read(new File("F:/新建文件夹/TestDom4j/src/com/gengu/Student.xml"));Element root = document.getRootElement();parse(root);}public static void parse(Element element){//处理当前元素包含的所有属性parseAttribute(element);//获取当前元素包含的所有子元素@SuppressWarnings("rawtypes")List el = element.elements();//遍历每个子元素for(Object e: el){Element element2 = (Element)e;if(!element2.isTextOnly()){parse(element2);}else {parseAttribute(element2);System.out.println(element2.getQName().getName()+"----->"+element2.getText());}}}//定义一个方法处理元素的所有属性public static void parseAttribute(Element ele){//获取Element元素的所有属性@SuppressWarnings("rawtypes")List attList = ele.attributes();for(Object e:attList){Attribute attribute = (Attribute)e;System.out.println(ele.getQName().getName()+"元素的"+attribute.getQName().getName() + "的属性知为:"+attribute.getValue());}}}


下面一种是使用访问者模式来遍历XML文档

dom4j的Node接口是一个根接口,它定义了一个accept(Visitor visitor)方法。

package com.gengu;import java.io.File;import java.io.IOException;import org.dom4j.*;import org.dom4j.io.XPP3Reader;import org.xmlpull.v1.XmlPullParserException;/** * 使用访问者模式遍历xml文档 * dom4j的Node接口中定义了一个accept(Visitor visitor)方法,该方法是在node接口中定义的  * */public class test3 {public static void main(String[] args) throws DocumentException, IOException, XmlPullParserException {//使用XPP3Reader来解析XML文档XPP3Reader reader = new XPP3Reader();Document document = reader.read(new File("F:/新建文件夹/TestDom4j/src/com/gengu/Student.xml"));document.accept(new YeekuVisitor());}}class YeekuVisitor extends VisitorSupport{//保存当前正在处理的节点private String currentElement;//当Visitor访问元素时回调该方法public void visit(Element node){//如果节点内容全部是文本if(node.isTextOnly()){System.out.println(node.getName() + "元素的信息" + node.getText());}currentElement = node.getName();}//当Visitor访问属性时回调该方法public void visit(Attribute node){System.out.println(currentElement + "元素的"+ node.getName() + "属性的值是:"+ node.getText());}//当Visitor访问处理指令的时候回调该方法public void visit(ProcessingInstruction node){System.out.println("处理指令"+ node.getTarget() + "的内容是:"+ node.getText());}}


至于什么时候访问者模式,我讲在模式博客中仔细描述。

 

使用dom4j创建xml文档

使用dom4j创建xml文档,也很简单,只需要记住基本的Element,attribute即可。一个实例如下


package com.gengu;import java.io.FileWriter;import java.io.IOException;import org.dom4j.*;import org.dom4j.io.HTMLWriter;import org.dom4j.io.OutputFormat;import org.dom4j.io.XMLWriter;public class test2 {public static void main(String[] args) throws IOException {//创建一个DocumentFactory duixiang DocumentFactory factory = new DocumentFactory();//创建一个Document对象Document document = factory.createDocument();//向Document对象中添加一个处理指令节点//document.addProcessingInstruction("crazyit", "website=\"http://www.crazyit.org\"");//向doc中添加根节点元素Element root = document.addElement("书籍列表");//采用循环的方式添加四个子元素for(int i = 0 ;i<4;i++){//创建一个计算机书籍子元素Element pcBook = root.addElement("计算机书籍");//添加一个随机数作为isbn的属性Element name = pcBook.addElement("名字");name.setText("编程思想");}OutputFormat  format = new OutputFormat("   ",true,"gbk");//FileWriter fwFileWriter = new FileWriter("F:/book.XHTML");FileWriter fw = new FileWriter("F:/book.xml");//定义一份XLWriter对象//HTMLWriter writer = new HTMLWriter(fw, format);XMLWriter writer = new XMLWriter(fw,format);writer.write(document);fw.close();}}


以上就是使用dom4j解析和创建XML文档,当然也可以处理成HTMLWriter,的以输出XHTML格式的。

 

总之,使用dom4j解析XML数据是一件非常简单的事情,它的操作很固定。


0 0
原创粉丝点击