配色: 字号:
在java中使用dom4j解析xml(示例代码)
2016-10-08 | 阅:  转:  |  分享 
  
在java中使用dom4j解析xml(示例代码)

鉴于目前的趋势,我们这里来讲讲Dom4j的基本用法,不涉及递归等复杂操作。Dom4j的用法很多,官网上的示例有那么点儿晦涩,这里就不写了

虽然Java中已经有了Dom和Sax这两种标准解析方式

但其操作起来并不轻松,对于我这么一个初学者来说,其中部分代码是活生生的恶心

为此,伟大的第三方开发组开发出了Jdom和Dom4j等工具

鉴于目前的趋势,我们这里来讲讲Dom4j的基本用法,不涉及递归等复杂操作

Dom4j的用法很多,官网上的示例有那么点儿晦涩,这里就不写了

首先我们需要出创建一个xml文档,然后才能对其解析

xml文档:

复制代码代码如下:

HarryPotterJK.RowlingLearningXMLErikT.Ray

示例一:用List列表的方式来解析xml

复制代码代码如下:

importjava.io.File;importjava.util.List;

importorg.dom4j.Attribute;importorg.dom4j.Document;importorg.dom4j.Element;importorg.dom4j.io.SAXReader;

publicclassDemo{

publicstaticvoidmain(String[]args)throwsException{SAXReaderreader=newSAXReader();Filefile=newFile("books.xml");Documentdocument=reader.read(file);Elementroot=document.getRootElement();ListchildElements=root.elements();for(Elementchild:childElements){//未知属性名情况下/ListattributeList=child.attributes();for(Attributeattr:attributeList){System.out.println(attr.getName()+":"+attr.getValue());}///已知属性名情况下System.out.println("id:"+child.attributeValue("id"));//未知子元素名情况下/ListelementList=child.elements();for(Elementele:elementList){System.out.println(ele.getName()+":"+ele.getText());}System.out.println();///已知子元素名的情况下System.out.println("title"+child.elementText("title"));System.out.println("author"+child.elementText("author"));//这行是为了格式化美观而存在System.out.println();}}

}

示例二:使用Iterator迭代器的方式来解析xml

复制代码代码如下:

importjava.io.File;importjava.util.Iterator;

importorg.dom4j.Attribute;importorg.dom4j.Document;importorg.dom4j.Element;importorg.dom4j.io.SAXReader;

publicclassDemo{publicstaticvoidmain(String[www.hunanwang.net]args)throwsException{SAXReaderreader=newSAXReader();Documentdocument=reader.read(newFile("books.xml"));Elementroot=document.getRootElement();Iteratorit=root.elementIterator();while(it.hasNext()){Elementelement=(Element)it.next();//未知属性名称情况下/IteratorattrIt=element.attributeIterator();while(attrIt.hasNext()){Attributea=(Attribute)attrIt.next();System.out.println(a.getValue());}///已知属性名称情况下System.out.println("id:"+element.attributeValue("id"));//未知元素名情况下/IteratoreleIt=element.elementIterator();while(eleIt.hasNext()){Elemente=(Element)eleIt.next();System.out.println(e.getName()+":"+e.getText());}System.out.println();///已知元素名情况下System.out.println("title:"+element.elementText("title"));System.out.println("author:"+element.elementText("author"));System.out.println();}}}

运行结果:



示例三:创建xml文档并输出到文件

复制代码代码如下:

importjava.io.File;importjava.io.FileOutputStream;

importorg.dom4j.Document;importorg.dom4j.DocumentHelper;importorg.dom4j.Element;importorg.dom4j.io.OutputFormat;importorg.dom4j.io.XMLWriter;

publicclassDemo{publicstaticvoidmain(String[www.visa158.com]args)throwsException{Documentdoc=DocumentHelper.createDocument();//增加根节点Elementbooks=doc.addElement("books");//增加子元素Elementbook1=books.addElement("book");Elementtitle1=book1.addElement("title");Elementauthor1=book1.addElement("author");Elementbook2=books.addElement("book");Elementtitle2=book2.addElement("title");Elementauthor2=book2.addElement("author");//为子节点添加属性book1.addAttribute("id","001");//为元素添加内容title1.setText("HarryPotter");author1.setText("JK.Rowling");book2.addAttribute("id","002");title2.setText("LearningXML");author2.setText("ErikT.Ray");//实例化输出格式对象OutputFormatformat=OutputFormat.createPrettyPrint();//设置输出编码format.setEncoding("UTF-8");//创建需要写入的File对象Filefile=newFile("D:"+File.separator+"books.xml");//生成XMLWriter对象,构造函数中的参数为需要输出的文件流和格式XMLWriterwriter=newXMLWriter(newFileOutputStream(file),format);//开始写入,write方法中包含上面创建的Document对象writer.write(doc);}}

运行结果:

























献花(0)
+1
(本文系白狐一梦首藏)