XML解析 Dom4j

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

  1. /**Dom4j方式,创建XML*/
  2. publicStringdom4jXMLCreate(){
  3. StringWriterxmlWriter=newStringWriter();
  4. Person[]persons=newPerson[3];//创建节点Person对象
  5. persons[0]=newPerson(1,"sunboy_2050","http://blog.csdn.net/sunboy_2050");
  6. 1]=newPerson(2,"baidu","http://www.baidu.com");
  7. 2]=newPerson(3,"google","http://www.google.com");
  8. try{
  9. org.dom4j.Documentdoc=DocumentHelper.createDocument();
  10. doc.setXMLEncoding("utf-8");
  11. org.dom4j.ElementeleRoot=doc.addElement("root");
  12. eleRoot.addAttribute("author","homer");
  13. eleRoot.addAttribute("date","2012-04-25");
  14. eleRoot.addComment("dom4jtest");
  15. intpersonsLen=persons.length;
  16. for(inti=0;i<personsLen;i++){
  17. ElementelePerson=eleRoot.addElement("person");//创建person节点,引用类为org.dom4j.Element
  18. ElementeleId=elePerson.addElement("id");
  19. eleId.addText(persons[i].getId()+"");
  20. ElementeleName=elePerson.addElement("name");
  21. eleName.addText(persons[i].getName());
  22. ElementeleBlog=elePerson.addElement("blog");
  23. eleBlog.addText(persons[i].getBlog());
  24. }
  25. org.dom4j.io.OutputFormatoutputFormat=neworg.dom4j.io.OutputFormat();//设置xml输出格式
  26. outputFormat.setEncoding("utf-8");
  27. outputFormat.setIndent(false);
  28. outputFormat.setNewlines(true);
  29. outputFormat.setTrimText(true);
  30. org.dom4j.io.XMLWriteroutput=newXMLWriter(xmlWriter,outputFormat);//保存xml
  31. output.write(doc);
  32. output.close();
  33. }catch(Exceptione){
  34. e.printStackTrace();
  35. }
  36. savedXML(fileName,xmlWriter.toString());
  37. returnxmlWriter.toString();
  38. }

运行结果:




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

?
    /**Dom4j方式,解析XML*/
  1. publicStringdom4jXMLResolve(){
  2. InputStreamis=readXML(fileName);
  3. SAXReaderreader=newSAXReader();
  4. org.dom4j.Documentdoc=reader.read(is);
  5. List<Person>personsList=null;
  6. Personperson=null;
  7. StringBufferxmlHeader=newStringBuffer();
  8. ElementeleRoot=doc.getRootElement();//获得root根节点,引用类为org.dom4j.Element
  9. StringattrAuthor=eleRoot.attributeValue("author");
  10. StringattrDate=eleRoot.attributeValue("date");
  11. xmlHeader.append("root").append("\t\t");
  12. xmlHeader.append(attrAuthor).append("\t");
  13. xmlHeader.append(attrDate).append("\n");
  14. personsList=newArrayList<Person>();
  15. //获取root子节点,即person
  16. Iterator<Element>iter=eleRoot.elementIterator();
  17. for(;iter.hasNext();){
  18. ElementelePerson=(Element)iter.next();
  19. if("person".equals(elePerson.getName())){
  20. person=newPerson();
  21. //获取person子节点,即id、name、blog
  22. Iterator<Element>innerIter=elePerson.elementIterator();
  23. for(;innerIter.hasNext();){
  24. Elementele=(Element)innerIter.next();
  25. if("id".equals(ele.getName())){
  26. Stringid=ele.getText();
  27. person.setId(Integer.parseInt(id));
  28. }elseif("name".equals(ele.getName())){
  29. Stringname=ele.getText();
  30. person.setName(name);
  31. }elseif("blog".equals(ele.getName())){
  32. Stringblog=ele.getText();
  33. person.setBlog(blog);
  34. personsList.add(person);
  35. person=null;
  36. xmlWriter.append(xmlHeader);
  37. intpersonsLen=personsList.size();
  38. xmlWriter.append(personsList.get(i).toString());
  39. }catch(DocumentExceptione){
  40. e.printStackTrace();
  41. }catch(Exceptione){
  42. }

运行结果:



解析二:选择性解析(XPath路径

Dom4j+XPath,选择性只解析id,doc.selectNodes("//root//person//id")

Code

?
    /**Dom4j方式,解析XML(方式二)*/
  1. publicStringdom4jXMLResolve2(){
  2. org.dom4j.io.SAXReaderreader=neworg.dom4j.io.SAXReader();
  3. @SuppressWarnings("unchecked")
  4. List<Element>idList=(List<Element>)doc.selectNodes("//root//person//id");//选择性获取全部id
  5. Iterator<Element>idIter=idList.iterator();
  6. while(idIter.hasNext()){
  7. ElementidEle=(Element)idIter.next();
  8. Stringid=idEle.getText();
  9. person.setId(Integer.parseInt(id));
  10. personsList.add(person);
  11. xmlWriter.append(xmlHeader);
  12. intpersonsLen=personsList.size();
  13. 0;i<personsLen;i++){
  14. xmlWriter.append("id=").append(personsList.get(i).getId()+"").append("\n");
  15. }catch(DocumentExceptione){
  16. returnxmlWriter.toString();
  17. }

注:借助 XPath 解析 XML 时,需要导入jaxen;本示例需要导入的是最新的jaxen包jaxen-1.1.3.jar,可以下载本示例下面【源码下载】或 访问jaxen jar

Jaxenis an open source XPath library written in Java. It is adaptable to many different object models,including DOM,XOM,dom4j,and JDOM. Is it also possible to write adapters that treat non-XML trees such as compiled Java byte code or Java beans as XML,thus enabling you to query these trees with XPath too.

jaxen 官方网址:jaxen

jaxen下载jar包:jaxen jarjaxen jar

jaxen源码查看:jaxen srcjaxen trunk


运行结果:





4、Person类

请参见前面博客Android 创建与解析XML(二)—— Dom方式【4、Person类】



源码下载

相关文章

引言 NOKIA 有句著名的广告语:“科技以人为本”。任何技术都是为了满足人的生产生活需要而产生的。具体...
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章...
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章...
http://blog.jobbole.com/79252/ 引言 NOKIA 有句著名的广告语:“科技以人为本”。任何技术都是为了满...
(点击上方公众号,可快速关注) 公众号:smart_android 作者:耿广龙|loonggg 点击“阅读原文”,可查看...
一、xml与xslt 相信所有人对xml都不陌生,其被广泛的应用于数据数据传输、保存与序列化中,是一种极为强...