用SAXReader解析xml文档本篇文章来源于:开发学院 http://edu.codepub.com 原文链接:http://edu.codepub.com/2009/1217/18796.php
来源:互联网 发布:php cookie session 编辑:程序博客网 时间:2024/05/13 23:52
使用SAXReader需要导入dom4j-full.jar包。 dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的。dom4j是一个非常非常优秀的Java XML API,具有性能优异、功能强大和极端易用使用的特点,同时它也是一个开放源代码的软件,可以在SourceForge上找到它。 使用举例:1. s.xml内容view plaincopy to clipboardprint?
2.解析view plaincopy to clipboardprint?import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Iterator; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.dom4j.tree.AbstractAttribute; public class ReadXMLTest { public static void main(String[] args){ File xmlFile = new File("C:/s.xml"); FileInputStream fis = null; try { fis = new FileInputStream(xmlFile); } catch (FileNotFoundException e) { e.printStackTrace(); System.err.println("File is not exsit!"); } SAXReader saxReader = new SAXReader(); List rowList = null; try { //生成文档对应实体 Document doc = saxReader.read(fis); //获取指定路径下的元素列表,这里指获取所有的data下的row元素 rowList = doc.selectNodes("//data/row"); } catch (DocumentException e) { e.printStackTrace(); } for(Iterator iter = rowList.iterator();iter.hasNext();){ //获得具体的row元素 Element element = (Element)iter.next(); //获得row元素的所有属性列表 List elementList = element.attributes(); for(Iterator iter1 = elementList.iterator();iter1.hasNext();){ //将每个属性转化为一个抽象属性,然后获取其名字和值 AbstractAttribute aa = (AbstractAttribute)iter1.next(); System.out.println("Name:"+aa.getName()+";Value:"+aa.getValue()); } //输出: //Name:queryDTO.enterpriseId;Value:gfd //Name:queryDTO.loginName;Value:gdfg //Name:queryDTO.state;Value:0 System.out.println(element.getName()); //输出: //row // 取得row元素的queryDTO.enterpriseId属性的值 System.out.println(element.attributeValue("queryDTO.enterpriseId")); //输出: //gfd //如果element下有子元素,(类似width="**"),要想获得该子元素的值,可以用如下方法 System.out.println(element.elementText("width"));//因为没有,所以输出为null。 } } } 本篇文章来源于:开发学院 http://edu.codepub.com 原文链接:http://edu.codepub.com/2009/1217/18796.php
- 用SAXReader解析xml文档本篇文章来源于:开发学院 http://edu.codepub.com 原文链接:http://edu.codepub.com/2009/1217/18796.php
- 不错的介绍JS中Cookie的文章本篇文章来源于:开发学院 http://edu.codepub.com 原文链接:http://edu.codepub.com/2009/0528/4789.php
- 安装 MySQL与MySQL GUI Tools本篇文章来源于:开发学院 http://edu.codepub.com 原文链接:http://edu.codepub.com/2009/0724/10870.php
- Zend Studio 实用快捷键一览表本篇文章来源于:开发学院 http://edu.codepub.com 原文链接:http://edu.codepub.com/2009/0618/6532.php
- 自动备份sql server2005数据库本篇文章来源于:开发学院 http://edu.codepub.com 原文链接:http://edu.codepub.com/2011/0323/30321.php
- ORACLE中文排序及在table中指定位置增加字段本篇文章来源于:开发学院 http://edu.codepub.com 原文链接:http://edu.codepub.com/2009/0531/5086.php
- http://edu.codepub.com/2010/1108/27051.php
- Android开发:用getDrawingCache方法获取ImageView中的图像需要注意的问题 本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http:/
- Linux下用内存管理器的钩子函数跟踪内存泄漏 本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/200
- session和cookie的区别 本篇文章来源于 站长资讯网 原文链接:http://www.chinahtml.com/1007/128010707619425.html
- Ubuntu Server 14.04 安装Web服务器(Linux+Apache+MySQL+PHP) 本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http
- Ubuntu 10.04地址导航栏修改为显示路径 本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/20
- Linux内核部件分析 更强的链表klist 本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/2011
- Linux中的spinlock和mutex 本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/2011-
- Windows XP硬盘安装Ubuntu 11.10双系统全程图解 本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com
- Linux下/dev/mem和/dev/kmem的区别 本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux
- Linux之TQ2440通过nfs挂载根文件系统 本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/20
- 在U-boot下实现自动识别启动Flash的原理 本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/201
- Java常识
- 深入了解VC编译器
- JSON定义(转)
- 第一篇
- Spring2.5+Struts2+Ibatis开发框架搭建
- 用SAXReader解析xml文档本篇文章来源于:开发学院 http://edu.codepub.com 原文链接:http://edu.codepub.com/2009/1217/18796.php
- 怎么定义二维数组呢
- 4.15
- 将舞台焦点赋给一个对象stage.focus = ball;
- ./configure make make install分别是什么意思?
- AIX Core Dump 分析
- 制作Thinkpad五合一WIN7光盘(只要两个小步骤,并附$OEM$资料包下载)
- 数据挖掘技术的算法与应用
- C++的多态与适配器模式