DOM解析XML文件

来源:互联网 发布:手机网络dns被劫持 编辑:程序博客网 时间:2024/06/10 21:45

DOM解析原理:

xml解析器一次性的吧整个xml文件加载进内存,然后在内存中构建一颗Document的对象树,通过Document对象,得到树上的节点对象,通过节点对象访问(操作)到xml文件的内容。


contact.xml文件:

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?><contactList><contact id="001"><name>张三</name><age>20</age><phone>134222223333</phone><email>zhangsan@qq.com</email><qq>432221111</qq></contact><contact id="002"><name>李四</name><age>20</age><phone>134222225555</phone><email>lisi@qq.com</email><qq>432222222</qq></contact></contactList></span>


Contact.java类文件

<span style="font-size:18px;">package com.lee.springmvc.testXml;public class Contact {<span style="white-space:pre"></span>private String id;<span style="white-space:pre"></span>private String name;<span style="white-space:pre"></span>private String age;<span style="white-space:pre"></span>private String phone;<span style="white-space:pre"></span>private String email;<span style="white-space:pre"></span>private String qq;<span style="white-space:pre"></span>public String getId() {<span style="white-space:pre"></span>return id;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public void setId(String id) {<span style="white-space:pre"></span>this.id = id;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public String getName() {<span style="white-space:pre"></span>return name;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public void setName(String name) {<span style="white-space:pre"></span>this.name = name;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public String getAge() {<span style="white-space:pre"></span>return age;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public void setAge(String age) {<span style="white-space:pre"></span>this.age = age;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public String getPhone() {<span style="white-space:pre"></span>return phone;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public void setPhone(String phone) {<span style="white-space:pre"></span>this.phone = phone;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public String getEmail() {<span style="white-space:pre"></span>return email;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public void setEmail(String email) {<span style="white-space:pre"></span>this.email = email;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public String getQq() {<span style="white-space:pre"></span>return qq;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>public void setQq(String qq) {<span style="white-space:pre"></span>this.qq = qq;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>@Override<span style="white-space:pre"></span>public String toString() {<span style="white-space:pre"></span>return "Contact [age=" + age + ", email=" + email + ", id=" + id<span style="white-space:pre"></span>+ ", name=" + name + ", phone=" + phone + ", qq=" + qq + "]";<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}</span>

JieXiXml.java

</pre><pre name="code" class="java"><span style="font-size:18px;">package com.lee.springmvc.testXml;import java.io.File;import java.util.ArrayList;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;/*** * 三种方法解析XML * DOM解析:dom4j-1.6.1.jar * SAX解析: * @author liyintao * */public class JieXiXml {public static void main(String[] args) throws Exception{new JieXiXml().jiexiXml();}/*** * 读取XML文件,并解析XML文件,在控制台打印 * @return */@SuppressWarnings("unchecked")public List<Contact> jiexiXml(){//读取xml,封装对象SAXReader reader = new SAXReader();String pathFile = System.getProperty("user.dir")+"/xmlFile/contact.xml";//Document doc = reader.read("./xmlFile/contact.xml");Document doc;List<Contact> list = new ArrayList<Contact>();try {doc = reader.read(pathFile);//读取contact标签//Iterator<Element> it = doc.getRootElement().elementIterator("contact");Element element = doc.getRootElement();Iterator<Element> it = element.elementIterator("contact");while(it.hasNext()){//判断是否有下一个元素Element e = it.next();//取出元素//创建 Contact对象Contact contact = new Contact();contact.setId(e.attributeValue("id"));//attributeValue() 元素的某个指定属性所含的值contact.setName(e.elementText("name"));//elementText() 元素的某个指定(qualified name或者local name)的子元素中的text信息contact.setAge(e.elementText("age"));contact.setEmail(e.elementText("email"));contact.setPhone(e.elementText("phone"));contact.setQq(e.elementText("qq"));list.add(contact);}for (Contact c : list) {System.out.println(c);}} catch (DocumentException e1) {e1.printStackTrace();}return list;}}</span>


补充一下迭代器

<span style="font-size:18px;">迭代器(Iterator)  迭代器是一种设计模式,它是一个对象,它可以遍历并选择序列中的对象,而开发人员不需要了解该序列的底层结构。迭代器通常被称为“轻量级”对象,因为创建它的代价小。  Java中的Iterator功能比较简单,并且只能单向移动:  (1) 使用方法iterator()要求容器返回一个Iterator。第一次调用Iterator的next()方法时,它返回序列的第一个元素。注意:iterator()方法是java.lang.Iterable接口,被Collection继承。  (2) 使用next()获得序列中的下一个元素。  (3) 使用hasNext()检查序列中是否还有元素。  (4) 使用remove()将迭代器新返回的元素删除。  Iterator是Java迭代器最简单的实现,为List设计的ListIterator具有更多的功能,它可以从两个方向遍历List,也可以从List中插入和删除元素。迭代器应用: list l = new ArrayList(); l.add("aa"); l.add("bb"); l.add("cc"); for (Iterator iter = l.iterator(); iter.hasNext();) {  String str = (String)iter.next();  System.out.println(str); } /*迭代器用于while循环 Iterator iter = l.iterator(); while(iter.hasNext()){  String str = (String) iter.next();  System.out.println(str); } */</span>



0 0
原创粉丝点击