Dom4j解析XML文档
2015-07-30 15:49
405 查看
Dom4j is 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.
创建XMLFile对象,创建SAXReader对象,获取document根节点
从根节点开始递归遍历所有节点
解析结果
写在后面
dom4j合并了许多超出基本XML文档表示的功能,包括集成的XPath支持、XML Schema支持以及用于大文档或流化文档的基于事件的处理。
测试文档 students.xml
<?xml version="1.0" encoding="GBK"?> <students class="一班" count="3"> <student> <name>小明</name> <age>10</age> </student> <student position="班长"> <name>小汪</name> <age>11</age> </student> <student> <name>小兵</name> <age>12</age> </student> </students>
创建XMLFile对象,创建SAXReader对象,获取document根节点
public void paserXML(){ try { /*创建SAXReader读取器,用来读取XML文档*/ SAXReader sax = new SAXReader(); /*创建XMLFile对象*/ File xmlFile = new File("students.xml"); /*通过file对象来读取XML文档*/ Document document = sax.read(xmlFile); /*获得根节点*/ Element rootele = document.getRootElement(); /*从根节点开始遍历所有节点*/ this.getNodes(rootele); } catch (DocumentException e) { System.err.println("文档没有节点..."); } }
从根节点开始递归遍历所有节点
private void getNodes(Element ele) { // TODO Auto-generated method stub System.out.println("------------------------------------------------"); /*当前节点的名称、文本内容和属性值*/ System.out.println("节点名称: " + ele.getName()); if(ele.getTextTrim().isEmpty()){ System.out.println("节点没有内容..."); }else{ System.out.println("节点的内容:" + ele.getTextTrim()); } /*当前节点的所有属性存放到list中*/ List<Attribute> listAttr = ele.attributes(); /*遍历当前节点的所有属性*/ for(Attribute attr : listAttr){ String name = attr.getName(); String value = attr.getValue(); System.out.println("属性名称: " + name + " 属性值: " + value); } /*递归遍历当前节点的所有子节点*/ List<Element> listele = ele.elements(); for(Element e : listele){ /*递归*/ this.getNodes(e); } }
解析结果
------------------------------------------------ 当前节点名称: students 当前节点没有内容... 属性名称: class 属性值: 一班 属性名称: count 属性值: 3 ------------------------------------------------ 当前节点名称: student 当前节点没有内容... ------------------------------------------------ 当前节点名称: name 当前节点的内容:小明 ------------------------------------------------ 当前节点名称: age 当前节点的内容:10 ------------------------------------------------ 当前节点名称: student 当前节点没有内容... 属性名称: position 属性值: 班长 ------------------------------------------------ 当前节点名称: name 当前节点的内容:小汪 ------------------------------------------------ 当前节点名称: age 当前节点的内容:11 ------------------------------------------------ 当前节点名称: student 当前节点没有内容... ------------------------------------------------ 当前节点名称: name 当前节点的内容:小兵 ------------------------------------------------ 当前节点名称: age 当前节点的内容:12
写在后面
dom4j合并了许多超出基本XML文档表示的功能,包括集成的XPath支持、XML Schema支持以及用于大文档或流化文档的基于事件的处理。
相关文章推荐
- Android视图TMD感受
- HTML-移动端如何使用css让百分比布局的弹窗水平和垂直方向上居中
- C#生成Code39条形码【非条形码字体】
- JQUERY操作JSON
- C#生成Code39条形码【非条形码字体】
- jquery append 动态添加的元素事件on 不起作用的解决方案
- c++中容器之总结篇
- 有关 Python 2 和 Sublime Text 中文 Unicode 编码问题的分析与理解
- android studio基本使用零碎整理
- c++之string类编写
- Android get/post方式请求网络设置超时
- iOS Xib下进行多国语言的国际化图解
- Android下得到Home键按下的消息
- 字符串
- shell中sed用法
- poj 1064 Cable master【浮点型二分查找】
- 如何将你的程序打包成ipa
- unity, setting standard shader by script
- oracle中的job
- R语言学习笔记