XML解析 Dom4j

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

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

运行结果:



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

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

Code

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

注:借助 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类】



源码下载

猜你在找的XML相关文章