使用SAX读取XML文件--(将信息封装到对象)

前端之家收集整理的这篇文章主要介绍了使用SAX读取XML文件--(将信息封装到对象)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

实现功能:使用SAX读取contact.xml文件,并将读取到的信息封装到对象中,然后输出

XML文件:contact.xml

<?xml version="1.0" encoding="utf-8"?>
<contactList>
	<contact id="001" name="eric">
		<name>张三</name>
		<age>20</age>
		<phone>134222223333</phone>
		<email>zhangsan@qq.com</email>
		<qq>432221111</qq>
	</contact>
	<contact id="002" name="jacky">
		<name>eric</name>
		<age>20</age>
		<phone>134222225555</phone>
		<email>lisi@qq.com</email>
		<qq>432222222</qq>
	</contact>
</contactList>


对象类:Contact.java

package SAX;

public class Contact {
	public String id;
	public String name;
	public String age;
	public String phone;
	public String email;
	public String qq;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getQq() {
		return qq;
	}
	public void setQq(String qq) {
		this.qq = qq;
	}
	@Override
	public String toString() {
		return "Contact [age=" + age + ",email=" + email + ",id=" + id
				+ ",name=" + name + ",phone=" + phone + ",qq=" + qq + "]";
	}
}

SAX的处理程序:MyDefaultHandler3.java

package SAX;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/*
 * SAX的处理程序
 */
public class MyDefaultHandler3 extends DefaultHandler {
	//存储所有的联系人的对象
	private List<Contact> list = new ArrayList<Contact>();
	public List<Contact> getList() {
		return list;
	}
	//保存一个联系人信息
	private Contact contact;
	//用于临时存储当前读到的标签名
	private String curTag;
	/*
	 *思路:
	 *	1)创建Contact对象
	 *	2)  把每个contact标签内容存入到Contact对象
	 *	3)把Contact对象放到List中
	 */

	public void startElement(String uri,String localName,String qName,Attributes attributes) throws SAXException {
		curTag = qName;
		//读取到contact的开始标签创建Contact对象
		if("contact".equals(qName)) {
			contact = new Contact();
			//设置id值
			contact.setId(attributes.getValue("id"));
		}
	}

	@Override
	public void characters(char[] ch,int start,int length)
			throws SAXException {
		//当前文本内容
		String content = new String(ch,start,length);
		if("name".equals(curTag)) {
			contact.setName(content);
		}
		
		if("age".equals(curTag)) {
			contact.setAge(content);
		}
		
		if("phone".equals(curTag)) {
			contact.setPhone(content);
		}
		
		if("email".equals(curTag)) {
			contact.setEmail(content);
		}
		
		if("qq".equals(curTag)) {
			contact.setQq(content);
		}
	}
	
	@Override
	public void endElement(String uri,String qName)
			throws SAXException {
		//设置空是为了避免空格和换行设置到对象时属性中
		curTag = null;
		//读到contact的结束标签放入List中
		if("contact".equals(qName)) {
			list.add(contact);
		}
	}
}

主程序:Demo_sax2.java

package SAX;

import java.io.File;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

public class Demo_sax2 {
	public static void main(String[] args) throws Exception {
		SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
		MyDefaultHandler3 handler = new MyDefaultHandler3();
		parser.parse(new File("./src/contact.xml"),handler);
		List<Contact> list = handler.getList();
		for(Contact contact : list) {
			System.out.println(contact);
		}
	}
}
原文链接:/xml/294566.html

猜你在找的XML相关文章