dom4j学习总结(一)

(一)创建Document的基本操作

/**
  * xml基本操作
  */
 public void BaseOperation(){
  //创建一个document
  Document document=DocumentHelper.createDocument();
  //创建根结点
  Element root=document.addElement("root");
  //为根结点添加一个book节点
  Element book1=root.addElement("book");
  //为book1添加属性type
  book1.addAttribute("type","science");
  //为book1添加name子节点
  Element name1=book1.addElement("Name");
  //并设置其name为"Java"
  name1.setText("Java");
  //为book1创建一个price节点,并设其价格为100
  book1.addElement("price").setText("100");
  
  //为根结点添加第二个book节点,并设置该book节点的type属性
  Element book2=root.addElement("book").addAttribute("type","science");
  //为book1添加name子节点
  Element name2=book2.addElement("Name");
  //并设置其name为"Oracle"
  name2.setText("Oracle");
  //为book1创建一个price节点,并设其价格为200
  book2.addElement("price").setText("200");
  
  //输出xml
  System.out.println(document.asXML());
 }

调用BaseOperation,输出结果为:

<?xml version="1.0" encoding="UTF-8"?>
<root>
 <book type="science">
  <Name>Java</Name>
  <price>100</price>
 </book>
 <book type="science">
  <Name>Oracle</Name>
  <price>200</price>
 </book>
</root>

(二)根据一个符合Document格式的字符串来生成一个Document

/**将字符串转化为Document
  * @param str  输入的字符串
  * @return  生成的document
  * @throws DocumentException
  */
 public Document parserStrtoDocument(String str) throws DocumentException{
  Document document=DocumentHelper.parseText(str);
  return document;
 }

调用示例:

String str="<root><book type='science'><Name>Java</Name><price>100</price></book></root>";

Document document = parserStrtoDocument(str);
  System.out.println(document.asXML());

输出结果为:

<?xml version="1.0" encoding="UTF-8"?>
<root>
 <book type="science">
  <Name>Java</Name>
  <price>100</price>
 </book>
</root>

(三)取得xml节点属性的基本方法

/**
  * 取得xml的节点和属性的值
  * @throws DocumentException 
  */
 public void getBaseInfofromDocument() throws DocumentException{
  String str="<root><book type='science'><Name>Java</Name><price>100</price></book></root>";
  //生成一个Document
  Document document = DocumentHelper.parseText(str);
  //取得根结点
  Element root=document.getRootElement();
  //取得book节点
  Element book=root.element("book");
  //取得book节点的type属性的值
  String type=book.attributeValue("type");
  //取得Name节点
  Element name=book.element("Name");
  //取得书名
  String bookname=name.getText();
  //取得书的价钱
  int price=Integer.parseInt(book.element("price").getText());
  
  //输出书目信息
  System.out.println("书名:"+bookname);
  System.out.println("所属类别:"+type);
  System.out.println("价格:"+price);
 }

调用getBaseInfofromDocument,输出结果为:

书名:Java
所属类别:science
价格:100

(四)利用迭代,xpath取得节点及其属性值

/**利用迭代,xpath取得xml的节点及其属性值
  * @throws DocumentException
  */
 public void getComplexInfofromDocument() throws DocumentException{

String str="<root><book type='science'><Name>Java</Name><price>100</price></book>"
   +"<book type='science'><Name>Oracle</Name><price>120</price></book>"
   +"<book type='society'><Name>Society security</Name><price>130</price></book>"
   +"<author><name>chb</name></author></root>";
  //生成一个Document
  Document document = DocumentHelper.parseText(str);
  
  //提取类型为"society"的书
  //此处需要添加支持xpath的jar包,详细见备注
  Element society_book=(Element)document.selectSingleNode("/root/book[@type='society']");
  System.out.println(society_book.asXML());
  
  //提取价格节点的列表
  System.out.println("-----------价格列表-------------");
  List price=document.selectNodes("//price");
  for(int i=0;i<price.size();i++){
   Element elem_price=(Element)price.get(i);
   System.out.println(elem_price.getText());
  }
  
  //循环根结点下的所有节点,若当前节点为book,则输出这本书的详细信息
  System.out.println("-------------书目详情------------");
  System.out.println("书名/t/t类别/t/t价格");
  Element root=document.getRootElement();
  Iterator iterator=root.elementIterator();
  while(iterator.hasNext()){
   Element element=(Element)iterator.next();
   if(element.getName().equals("book")){
    System.out.print(element.element("Name").getText()+"/t");
    System.out.print(element.attributeValue("type")+"/t/t");
    System.out.print(element.element("price").getText()+"/n");
   }
  }

//查找作者姓名
  Element author=(Element)document.selectSingleNode("//author");
  System.out.println("---------"+author.element("name").getText()+"----------");
  //提取作者的所有书目名称
  Iterator iterator_book=root.elementIterator("book");  
  while(iterator_book.hasNext()){
   Element book=(Element)iterator_book.next();
   System.out.print(book.element("Name").getText()+"/t");
  }
  
  //属性迭代
  System.out.println("/n-------属性迭代--------");
  String str1="<book type='science' name='Java' price='100'/>";
  Document document1=DocumentHelper.parseText(str1);
  //开始迭代
  Iterator iterator_attribute=document1.getRootElement().attributeIterator();
  while(iterator_attribute.hasNext()){
   //提取当前属性
   Attribute attribute=(Attribute)iterator_attribute.next();
   System.out.println(attribute.getName()+":"+attribute.getValue());
  }
 }

调用getComplexInfofromDocument,输出结果为:

<book type="society"><Name>Society security</Name><price>130</price></book>
-----------价格列表-------------
100
120
130
-------------书目详情------------
书名  类别  价格
Java science  100
Oracle science  120
Society security society  130
---------chb----------
Java Oracle Society security 
-------属性迭代--------
type:science
name:Java
price:100

备注:调用该方法之前,应该先向工程中添加支持xpath的jar包,否则,会出现以下错误:

java.lang.NoClassDefFoundError: org/jaxen/JaxenException
 at org.dom4j.DocumentFactory.createXPath(DocumentFactory.java:230)
 at org.dom4j.tree.AbstractNode.createXPath(AbstractNode.java:207)
 at org.dom4j.tree.AbstractNode.selectSingleNode(AbstractNode.java:183)
 at xml_chb.dom4j_chb.getComplexInfofromDocument(dom4j_chb.java:82)
 at xml_chb.dom4j_chb.main(dom4j_chb.java:92)
Exception in thread "main"

只需要引入jaxen包就行了,我使用的是hibernate包中的jaxen-1.1-beta-7.jar包。

dom4j学习总结(一)的更多相关文章

  1. dom4j学习

    在使用xml读写的过程中,用到了dom4j,也算是一个比较主流的xml包了,在使用的过程中,将学习经历记录一下,以后查阅也比较方便. 首先是在pom中添加依赖,在Maven的中心库搜索后选择了该包: ...

  2. dom4j 学习总结

    Dom4j is an easy to use, open source library for working with XML, XPath and XSLT on the Java platfo ...

  3. dom4J 学习

    Java给我们提供了标准的W3C接口实现,已完成对XML的处理.主要有两大类,分别是DOM操作,SAX解析.DOM可以将XML加载到内存中,对XML进行方便的增删查改.由于是将整个XML都加载到内存中 ...

  4. Dom4j 学习笔记

    dom4j 是一种解析 XML 文档的开放源代码 XML 框架.dom4j下载地址 本文主要记载了一些简单的使用方法. 一.xml文件的解析 dom4j既可以解析普通的xml文件,也可以解析一个Inp ...

  5. Dom4j学习笔记

    一.Loading XML Data 以下代码从File中或一个URL中读取一个XML文件,并产生一个Document对象.一个Document对象表示了内存中的一棵XML树,可以在这个XML树中进行 ...

  6. dom4j处理带命名空间的XML-使用XPath(转)

    dom4j处理带命名空间的XML-使用XPath 博客分类: XML   XPath 是一门在 XML 文档中查找信息的语言.XPath 可用来在 XML 文档中对元素和属性进行遍历. XPath 使 ...

  7. XML学习笔记(2)--dom4j操作XML

    1. 介绍(四种方式的比较这部分转载自:http://www.blogjava.net/xcp/archive/2010/02/12/312617.html) 1)DOM(JAXP Crimson解析 ...

  8. java学习笔记DOM4J解析(7)

    DOM4J即Document Object Model for Java使用java技术以文档方式解析XML数据的模型. DOM4J是开源组织提供的一个免费的.强大的XML解析工具,如果开发者需要在项 ...

  9. JavaWeb学习笔记——DOM4J

    下载的地址为:http://www.dom4j.org/dom4j-1.6.1/ import java.io.File; import java.io.FileOutputStream; impor ...

随机推荐

  1. 如何理解dart的mixin

    mixin翻译出来就是混入的意思 混入,就是一个类可以使用另一个类里的功能比如方法或者属性,其实这个功能并不陌生 ,有点类似c#里的扩展方法,但是并不同于, mixin和implements有着本质的 ...

  2. idea使用jrebel热部署插件

    首先通过idea下载JReble插件 访问http://idea.lanyus.com/网站 跟着弹出框的步骤走就可以实现了: 在idea中使用tomcat部署项目 不要使用war:使用下面的文件

  3. spring security的原理及教程

    spring security使用分类: 如何使用spring security,相信百度过的都知道,总共有四种用法,从简到深为:1.不用数据库,全部数据写在配置文件,这个也是官方文档里面的demo: ...

  4. <转>杜绝 Defunct进程 僵尸进程

    http://hanover.iteye.com/blog/881972 在测试基于 DirectFB+Gstreamer 的视频联播系统的一个 Demo 的时候,其中大量使用 system 调用的语 ...

  5. Delphi数据库的三层架构的问题和解决方法

    Delphi数据库的三层架构的问题和解决方法 原创 2014年03月26日 16:26:03 标签: Delphi / 数据库三层架构 / DCOM / DCOMConnection 790 //-- ...

  6. 【总结整理】自带天气app,为什么还要下载

    很简单那就说明用户对天气这个功能的需求并没有表面那么简单呗,还有更深层次的需求~ 先声明我自己是没有这方面需求的,我就纯属YY一下 既然数据都一样的话,那是什么让用户觉得天气APP更专业呢? 1.历史 ...

  7. eval 是执行一段完整的js字符串代码,并将结果返回

    var strArray="[{"message1":{ "id": "-1","content": &quo ...

  8. spring4-3-AOP-面向切面编程

    AOP常用的两个用户:日志和验证.也就是程序追踪和数据验证. 直接使用代码实现,距离如下:

  9. SQL 数据库 学习 003 什么是数据库? 为什么需要数据库?是不是所有的软件都是用Sql Server?

    什么是数据库? 为什么需要数据库? 是不是所有的软件都是用Sql Server? 我的电脑系统: Windows 10 64位 使用的SQL Server软件: SQL Server 2014 Exp ...

  10. CF 1097D Makoto and a Blackboard

    算是记一下昨天晚上都想了些什么 官方题解   点我 简单题意 给定两个正整数$n$和$k$,定义一步操作为把当前的数字$n$等概率地变成$n$的任何一个约数,求$k$步操作后的期望数字,模$1e9 + ...