`
webcode
  • 浏览: 5948560 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

dom4j学习总结(一)

 
阅读更多

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
-------------书目详情------------
书名类别价格
Javascience100
Oraclescience120
Society securitysociety130
---------chb----------
JavaOracleSociety 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很全的学习资料知识点讲解加上例子

    DOM4J很全的学习资料知识点讲解加上例子,总结的很详细。对学习DOM4J很有帮助,可作为入门 也可以作为参考资料

    Java+flex使用dom4j读写xml

    环境:MyEclipse6.0.1+Flex3插件版,jdk1.6,tomcat6.0,dom4j1.6.1,lcds.war, 使用技术:1.Java+flex,及其对象的转换。 2.Java方面,使用dom4j对xml进行操作,包括节点的读取,添加,修改,删除。 3.Flex方面...

    xml资料

    dom4j学习总结 以及练习

    xml笔记及操作代码

    自己总结的一份xml学习笔记。内容包括xml文件的书写格式,约束、jaxp及dom4j对xml解析的代码。

    Java语言基础下载

    DOM4J解析实例 412 JDOM解析实例 413 JAVA操纵XML 实例讲解 414 通过JAVA写数据到XML里面 415 内容总结 418 独立实践 418 第二十三章:HTML基础 419 学习目标 419 知识要点 420 HTML元素 420 标签属性 420 HTML基本...

    XML轻松学习手册--XML肯定是未来的发展趋势,不论是网页设计师还是网络程序员,都应该及时学习和了解

    4.DOM则为脚本和对象的交流提供一个公共平台,并将结果显示在浏览器窗口。 如果任何一个部分发生错误,都不会得到正确结果。 好了,看到这里,我们已经对XML是如何工作的有一个整体的大致的概念。通过这一章的...

    JQuery学习总结【二】

    一:JQuery知识点 *:JQuery的dom操作 *:动态创建dom节点 比如动态创建表格等,在js里面进行完成。 *删除节点 这里面的删除就是将其放在了一个地方,并不是真的删除,之后可以使用。 *:document方法 1:.val()...

    Maven权威指南 很精典的学习教程,比ANT更好用

    总结 2. 安装和运行Maven 2.1. 验证你的Java安装 2.2. 下载Maven 2.3. 安装Maven 2.3.1. 在Mac OSX上安装Maven 2.3.2. 在Microsoft Windows上安装Maven 2.3.3. 在Linux上安装Maven 2.3.4. 在FreeBSD或...

    详细的XML解析笔记

    本文档详细的给出了XML的介绍和XML解析的实例。包括DOM4J和SAX解析,节点的名和值得读取,属性的读取。生成XML文件等

    前端笔记.zip

    这个文档包含了HTML/css的一些基础,还有JavaScript中的基础语法、DOM、BOM还有一些学习js中面向对象、和移动web开发、AJAX、jQuery的一些总结,还有些Web前端与移动开发面试宝典; 6、什么是事件冒泡/捕获? 事件...

    Android中对xml文件解析的3种方式总结

    主要给大家介绍了关于Android中对xml文件解析的3种方式,分别是 Dom 、 SAX 和 dom4j,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。

    颜色分类leetcode-deep-learning-colonoscopy:结肠镜息肉检测和分类的深度学习综述

    颜色分类leetcode 结肠镜息肉检测和分类的深度学习 该存储库是根据以下评论论文创建的...最后,第四部分总结了每项研究报告的性能指标。 欢迎提出建议,请在提交拉取请求之前检查。 目录: 研究 息肉检测和定位 学习 日

    Java学习笔记-个人整理的

    {14.4}dom4j}{207}{section.14.4} {14.5}XPath}{210}{section.14.5} {14.6}apache.commons}{211}{section.14.6} {15}sqlite3}{213}{chapter.15} {16}Web基础}{215}{chapter.16} {16.1}...

Global site tag (gtag.js) - Google Analytics