当windows 7系统中程序和功能无法卸载IE9,10,11时的方法
2014-03-05 15:23
696 查看
解析XML的方式有两种:DOM和SAX
关于DOM有如下一个例子。
1.person.xml
2.Person.java
关于DOM有如下一个例子。
1.person.xml
<?xml version="1.0" encoding="UTF-8"?> <学生名册> <学生 学号="1"> <姓名>张三</姓名> <性别>男</性别> <年龄>20</年龄> </学生> <学生 学号="2"> <姓名>李四</姓名> <性别>女</性别> <年龄>19</年龄> </学生> <学生 学号="3"> <姓名>王五</姓名> <性别>男</性别> <年龄>21</年龄> </学生> </学生名册>
2.Person.java
package com.lijun.xml.dom; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; public class Person { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { //获得解析器工厂 DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); //获得解析器 DocumentBuilder builder=factory.newDocumentBuilder(); //获得根节点(persion.xml放到工程目录下) Document document=builder.parse(new File("person.xml")); NodeList nodeList=document.getElementsByTagName("学生"); for(int i=0;i<nodeList.getLength();i++){ Element element=(Element)nodeList.item(i); String content=element.getElementsByTagName("姓名").item(0).getFirstChild().getNodeValue(); System.out.println("姓名:"+content); content=element.getElementsByTagName("性别").item(0).getFirstChild().getNodeValue(); System.out.println("性别:"+content); content=element.getElementsByTagName("年龄").item(0).getFirstChild().getNodeValue(); System.out.println("年龄:"+content); System.out.println("-----------------------------------------"); } } }
相关文章推荐
- 福布斯富豪榜:24岁中国女孩成最年轻富豪
- Android:ADT 22.6..0
- linux根目录下各文件夹的内容
- cvInitFont
- java.lang.OutOfMemoryError: PermGen space及其解决方法
- Eclipse中文字体太小,设置字体
- windows phone (27) 基础Button
- 当windows 7系统中程序和功能无法卸载IE9,10,11时的方法
- 小记:platform总线注册设备和设备驱动
- 面向对象编程思想--继承
- Android基于XMPP Smack openfire 开发的聊天室(一)【会议服务、聊天室列表、加入】
- java 线性布局
- Xcode快捷键总结
- JDK1.7 G1垃圾回收器
- WebService框架整理(二) Axis1+Spring
- java中import和package的用法
- 树、森林与二叉树的转换
- 使用Jquery.load()方法,出现-此页的状态信息无效,可能已损坏。[转]
- topcoder srm 550
- linux expect 安装