代码运行前需要先导入dom4j架包。

需要解析的XML文件test.xml如下:

<students>
<student age="25"><!--如果没有age属性,默认的为20-->
<name>崔卫兵</name>
<college>PC学院</college>
<telephone>62354666</telephone>
<notes>男,1982年生,硕士,现就读于北京邮电大学</notes>
</student>
<student age="26">
<name>cwb</name>
<college leader="学院领导">PC学院</college><!--如果没有leader属性,默认的为leader-->
<telephone>62358888</telephone>
<notes>男,1987年生,硕士,现就读于中国农业大学</notes>
</student>
</students>

<1>、当测试文件test.xml在D盘时,Java程序代码如下:

package Test01;

import java.io.FileNotFoundException;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader; public class test01 { public void testRead() throws DocumentException, FileNotFoundException{
SAXReader reader=new SAXReader(); Document doc=reader.read("D:/test.xml");
Element root =doc.getRootElement();
for(@SuppressWarnings("rawtypes")
Iterator it=root.elementIterator();it.hasNext();){
Element element=(Element)it.next(); System.out.println(element.attribute("age").getName()+" == "+element.attribute("age").getValue());
System.out.println(element.attributeValue("age"));
System.out.println(element.getName());
for(@SuppressWarnings("rawtypes")
Iterator itt=element.elementIterator();itt.hasNext();){
//System.out.println(element.attributeValue("age"));
Element el=(Element)itt.next();
System.out.println(el.getName()+"=="+el.getText());
//getText()获取的是两个标签间的数据如"<name>崔卫兵</name>"中的崔卫兵
//getName()获取的是标签名,即“<student age="25"><name>崔卫兵</name>”中的age和name
//attributeValue("age")可获取age的值即25
}
System.out.println("==================");
}
} public static void main(String[] args){
try {
new test01().testRead();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

代码运行后控制台显示结果如下:

<2>、当测试文件test.xml和项目在一起时,Java程序代码如下:

package Test01;

import java.io.FileNotFoundException;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader; public class test01 { public static void testRead(String path) throws DocumentException, FileNotFoundException{
SAXReader reader=new SAXReader(); //Document doc=reader.read("D:/test.xml");
Document doc = reader.read(path);
Element root =doc.getRootElement();
for(@SuppressWarnings("rawtypes")
Iterator it=root.elementIterator();it.hasNext();){
Element element=(Element)it.next(); System.out.println(element.attribute("age").getName()+" == "+element.attribute("age").getValue());
System.out.println(element.attributeValue("age"));
System.out.println(element.getName());
for(@SuppressWarnings("rawtypes")
Iterator itt=element.elementIterator();itt.hasNext();){
//System.out.println(element.attributeValue("age"));
Element el=(Element)itt.next();
System.out.println(el.getName()+"=="+el.getText());
//getText()获取的是两个标签间的数据如"<name>崔卫兵</name>"中的崔卫兵
//getName()获取的是标签名,即“<student age="25"><name>崔卫兵</name>”中的age和name
//attributeValue("age")可获取age的值即25
}
System.out.println("==================");
}
} public static void main(String[] args){
// try {
// new test01().testRead();
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
try {
testRead("test.xml");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

代码运行后控制台显示结果如下:

java dom4j解析xml实例(3)的更多相关文章

  1. java dom4j解析xml实例(2)

    java利用dom4j解析xml 需要的jar包: dom4j官方网站在 http://www.dom4j.org/ 下载dom4j-1.6.1.zip 解开后有两个包,仅操作XML文档的话把dom4 ...

  2. java dom4j解析xml实例

    java利用dom4j解析xml 需要的jar包: dom4j官方网站在 http://www.dom4j.org/ 下载dom4j-1.6.1.zip 解开后有两个包,仅操作XML文档的话把dom4 ...

  3. java dom4j 解析xml使用实践

    参考:https://dom4j.github.io/ http://www.cnblogs.com/liuling/archive/2013/02/05/dom4jxml.html 常用api: 1 ...

  4. java dom4j解析xml用到的几个方法

    1. 读取并解析XML文档: SAXReader reader = new SAXReader(); Document document = reader.read(new File(fileName ...

  5. dom4j解析xml实例(2)

    dom4j是一个java的XML API,类似jdom,用来读写XML文件,它性能优异.功能强大和极易使用等特点 所用jar包:dom4j-1.6.1.jar.jaxen-1.1-beta-6.jar ...

  6. dom4j解析xml实例

    dom4j是一个java的XML API,类似jdom,用来读写XML文件,它性能优异.功能强大和极易使用等特点 所用jar包:dom4j-1.6.1.jar 需要解析的xml文件:people.xm ...

  7. JAVA DOM4j解析XML数据到自定义javabean

    我们获取xml中的数据,一般以面向对象的思想去处理这些数据.因此,我们需要自定义类来封装解析出来的数据,以方便我们操作这些数据. 自定义的java类,称为javabean. 自定义Contact类代码 ...

  8. 【收藏用】--切勿转载JAVA 使用Dom4j 解析XML

    原帖地址 : http://blog.csdn.NET/yyywyr/article/details/38359049 解析XML的方式有很多,本文介绍使用dom4j解析xml. 1.环境准备 (1) ...

  9. JAVA 使用Dom4j 解析XML

    [转自] http://blog.csdn.net/yyywyr/article/details/38359049 解析XML的方式有很多,本文介绍使用dom4j解析xml. 1.环境准备 (1)下载 ...

随机推荐

  1. memcached + php 扩展 for ubuntu

    1.安装memcached apt-get install memcached 2.安装php memcached 扩展 apt-get install php5-memcache 3.启动memca ...

  2. Spring的后置处理器BeanPostProcessor

    一.BeanPostProcessor接口的作用 如果我们需要在Spring容器完成Bean的实例化.配置和其他的初始化前后添加一些自己的逻辑处理,我们就可以定义一个或者多个BeanPostProce ...

  3. selenium+firefox时每次都要导入数据解决方法解决方法:

    火狐录制组件:selenium-ide-2.9.0.xpiselenium1调试c#前要运行服务器 selenium-server-standalone-2.47.1.jar导入引用压缩包seleni ...

  4. git基本命令--tag, alias,

    git tag: 列出标签 在 Git 中列出已有的标签是非常简单直观的. 只需要输入 git tag: $ git tag v0. v1. 这个命令以字母顺序列出标签:但是它们出现的顺序并不重要. ...

  5. hdu_5802_Windows 10(贪心)

    题目链接:hdu_5802_Windows 10 题意: 给你两个音量a,b,要让你将音量a变到音量b,up:每秒只能一次加1的音量,down:如果连续按x秒,那么就会减2x-1的音量 题解: 对于a ...

  6. hdu_5773_The All-purpose Zero(LIS)

    题目链接:hdu_5773_The All-purpose Zero 题意: 给你一串数,让你求LIS,不过这里的0可以改变为任意数 题解: 官方题解讲的很清楚 1010 The All-purpos ...

  7. hdu_1536_S-Nim(DFS_SG博弈)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1536 题意:首先输入K ,表示一个集合的大小 , 之后输入集合, 表示对于这对石子只能去这个集合中的元 ...

  8. hdu_2899_Strange fuction(三分查找)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2899 题意:让你解方程 题解:对于只有一个凸或者没有凸的图像,可以直接上三分解决. #include& ...

  9. LeetCode OJ 98. Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  10. R语言笔记2--循环、R脚本

    1.循环语句 for语句 while语句 2.R脚本 source()函数 print()函数