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
-------------书目详情------------
书名 类别 价格
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学习总结(一)的更多相关文章
- dom4j学习
在使用xml读写的过程中,用到了dom4j,也算是一个比较主流的xml包了,在使用的过程中,将学习经历记录一下,以后查阅也比较方便. 首先是在pom中添加依赖,在Maven的中心库搜索后选择了该包: ...
- dom4j 学习总结
Dom4j is an easy to use, open source library for working with XML, XPath and XSLT on the Java platfo ...
- dom4J 学习
Java给我们提供了标准的W3C接口实现,已完成对XML的处理.主要有两大类,分别是DOM操作,SAX解析.DOM可以将XML加载到内存中,对XML进行方便的增删查改.由于是将整个XML都加载到内存中 ...
- Dom4j 学习笔记
dom4j 是一种解析 XML 文档的开放源代码 XML 框架.dom4j下载地址 本文主要记载了一些简单的使用方法. 一.xml文件的解析 dom4j既可以解析普通的xml文件,也可以解析一个Inp ...
- Dom4j学习笔记
一.Loading XML Data 以下代码从File中或一个URL中读取一个XML文件,并产生一个Document对象.一个Document对象表示了内存中的一棵XML树,可以在这个XML树中进行 ...
- dom4j处理带命名空间的XML-使用XPath(转)
dom4j处理带命名空间的XML-使用XPath 博客分类: XML XPath 是一门在 XML 文档中查找信息的语言.XPath 可用来在 XML 文档中对元素和属性进行遍历. XPath 使 ...
- XML学习笔记(2)--dom4j操作XML
1. 介绍(四种方式的比较这部分转载自:http://www.blogjava.net/xcp/archive/2010/02/12/312617.html) 1)DOM(JAXP Crimson解析 ...
- java学习笔记DOM4J解析(7)
DOM4J即Document Object Model for Java使用java技术以文档方式解析XML数据的模型. DOM4J是开源组织提供的一个免费的.强大的XML解析工具,如果开发者需要在项 ...
- JavaWeb学习笔记——DOM4J
下载的地址为:http://www.dom4j.org/dom4j-1.6.1/ import java.io.File; import java.io.FileOutputStream; impor ...
随机推荐
- 关于data-属性
关于data-属性 现有需求如下,也就是类似做一个tab页的切换如下图: 因为这里要记录一下jquery里的“data-属性”的用法,所以忽略类似的组件. 往HTML标签上添加任意以 "da ...
- Elasticsearch-PHP 处理JSON数组和对象
PHP中处理JSON数组和对象 客户端有一些混淆的资源是围绕着JSON的数组和对象,以及如何在PHP中指定它们.特别是,问题是由空对象和空数组导致的.这篇文章回告诉你一些在Elasticsearch ...
- springmvc jpa
昨天帮同学搭建了一个springmvc+jpa+beetl模板引擎的项目环境,供参考. https://files.cnblogs.com/files/startnow/lntu-demo.zip 数 ...
- 使用AddressSanitizer做内存分析(一)——入门篇
使用AddressSanitizer做内存分析 新建文件mem_leak.cpp,键入代码: #include <iostream> int main() { ]; p = NULL; ; ...
- 获取崩溃时的调用栈和生成dump文件,然后自动重启
首先要说明的是: linux 下 比较方便可以得到 崩溃时的调用栈,win下 比较难办 1. linux 获取调用栈 代码奉上: #include <execinfo.h> //在头 ...
- c# ftp 上传文件 与 下载文件
接着上一篇说. 上一篇说了根据配置文件获取路径,并判断路径在服务器中是否存在.如果不存在则在服务器中建立一个. 然后就是往路径下面传输文件了.. 代码: //连接ftp private void Co ...
- 为什么要使用href=”javascript:void(0);”
为什么要使用href=”javascript:void(0);” href=”javascript:void(0);”这个的含义是,让超链接去执行一个js函数,而不是去跳转到一个地址,而void( ...
- http协议简析(一)
HTTP:hype-text transfer protocol,超文本传输协议,超文本(html)在网络间(电脑与电脑之间)传输过程中所遵循的一些规则. 两台电脑之间要实现数据传输的条件 1.两台电 ...
- Java中String类型详解
这篇博客是我一直想总结的,这两天一直比较忙,先上传下照片吧,过后有时间再弄成正常的. 本文主要是对Java中String类型的总结,包括其在JVM中是怎么存储的...
- [C++] Template Function _ Any number of parameters
Template Function _ Any number of parameters #include<iostream> #include<cstdarg> using ...