1、Dom4j概述
dom4jis an easy to use,open source library for working with XML,XPath and XSLT on the Java platform using the Java Collections Framework and with full support for DOM,SAX and JAXP.
dom4j官方网址:dom4j
dom4j源码下载:dom4jdownload
本示例中,需要导入dom4j.jar包,才能引用dom4j相关类,dom4j源码和jar包,请见本示例【源码下载】或访问dom4j
org.dom4j包,不仅包含创建xml的构建器类DocumentHelper、Element,而且还包含解析xml的解析器SAXReader、Element,包含类如下:
org.dom4j
org.dom4j.DocumentHelper;
org.dom4j.Element;
org.dom4j.io.SAXReader;
org.dom4j.io.XMLWriter;
org.dom4j.DocumentException;
sdk源码查看路径(google code)
创建和解析xml的效果图:
2、Dom4j 创建 XML
Dom4j,创建xml主要用到了org.dom4j.DocumentHelper、org.dom4j.Document、org.dom4j.io.OutputFormat、org.dom4j.io.XMLWriter
首先,DocumentHelper.createDocument(),创建org.dom4j.Document 的实例 doc
接着,通过doc,设置xml属性doc.setXMLEncoding("utf-8")、doc.addElement("root")根节点,以及子节点等
然后,定义xml格式并输出,new XMLWriter(xmlWriter,outputFormat)
Code
- @H_301_95@/**Dom4j方式,创建XML*/
- @H_301_95@publicStringdom4jXMLCreate(){
- @H_301_95@StringWriterxmlWriter=newStringWriter();
- @H_301_95@
- @H_301_95@Person[]persons=newPerson[3];//创建节点Person对象
- @H_301_95@persons[0]=newPerson(1,"sunboy_2050","http://blog.csdn.net/sunboy_2050");
- 1]=newPerson(2,"baidu","http://www.baidu.com");
- 2]=newPerson(3,"google","http://www.google.com");
- @H_301_95@
- @H_301_95@try{
- @H_301_95@org.dom4j.Documentdoc=DocumentHelper.createDocument();
- @H_301_95@doc.setXMLEncoding("utf-8");
- @H_301_95@org.dom4j.ElementeleRoot=doc.addElement("root");
- @H_301_95@eleRoot.addAttribute("author","homer");
- @H_301_95@eleRoot.addAttribute("date","2012-04-25");
- @H_301_95@eleRoot.addComment("dom4jtest");
- @H_301_95@intpersonsLen=persons.length;
- @H_301_95@for(inti=0;i<personsLen;i++){
- @H_301_95@ElementelePerson=eleRoot.addElement("person");//创建person节点,引用类为org.dom4j.Element
- @H_301_95@ElementeleId=elePerson.addElement("id");
- @H_301_95@eleId.addText(persons[i].getId()+"");
- @H_301_95@ElementeleName=elePerson.addElement("name");
- @H_301_95@eleName.addText(persons[i].getName());
- @H_301_95@ElementeleBlog=elePerson.addElement("blog");
- @H_301_95@eleBlog.addText(persons[i].getBlog());
- @H_301_95@}
- @H_301_95@org.dom4j.io.OutputFormatoutputFormat=neworg.dom4j.io.OutputFormat();//设置xml输出格式
- @H_301_95@outputFormat.setEncoding("utf-8");
- @H_301_95@outputFormat.setIndent(false);
- @H_301_95@outputFormat.setNewlines(true);
- @H_301_95@outputFormat.setTrimText(true);
- @H_301_95@org.dom4j.io.XMLWriteroutput=newXMLWriter(xmlWriter,outputFormat);//保存xml
- @H_301_95@output.write(doc);
- @H_301_95@output.close();
- @H_301_95@}catch(Exceptione){
- @H_301_95@e.printStackTrace();
- @H_301_95@}
- @H_301_95@savedXML(fileName,xmlWriter.toString());
- @H_301_95@returnxmlWriter.toString();
- @H_301_95@}
运行结果:
3、Dom4j 解析 XML
Dom4j,解析xml主要用到了org.dom4j.io.SAXReader、org.dom4j.Document、doc.getRootElement(),以及ele.getName()、ele.getText()等
首先,创建SAXReader的实例reader,读入xml字节流reader.read(is)
接着,通过doc.getRootElement()得到root根节点,利用迭代器取得root下一级的子节点eleRoot.elementIterator()等
然后,得到解析的xml内容xmlWriter.append(xmlHeader)、xmlWriter.append(personsList.get(i).toString())
解析一:标准解析(Iterator 迭代)
Code