欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SAX解析xml

程序员文章站 2022-05-28 09:38:19
...

SAXParseHandler.java

package com.hl.chapter1;

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

import javax.swing.text.AbstractDocument.Content;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SAXParseHandler extends DefaultHandler{
	
	//返回结果集
	List<User> users = null;
	//保存当前节点的内容
	String content = null;
	//保存当前的用户节点信息
	private User user = null;

	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
		//初始化结果集
		users = new ArrayList<>();
		System.out.println("开始解析--------");
	}

	@Override
	public void endDocument() throws SAXException {
		System.out.println("解析结束--------");
		super.endDocument();
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		//将一个子节点的属性保存
		if("user".equals(qName)){
			user = new User();
			String id = attributes.getValue("id");
			user.setId(id);
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		super.endElement(uri, localName, qName);
		//将一个节点的值保存
		if("name".equals(qName)){
			user.setName(content);
		}else if ("age".equals(qName)) {
			user.setAge(content);
		}else if ("num".equals(qName)) {
			user.setNum(content);
		}else if ("user".equals(qName)) {
			//将一个完整节点信息添加到结果集中
			users.add(user);
		}
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		super.characters(ch, start, length);
		//内容
		content = new String(ch,start,length);
		
	}
	
	public List<User> getUser(){
		//获取结果集
		return users;
	}

}

 

user.xml

<?xml version="1.0" encoding="UTF-8"?>
<users>
	<user id="1">
		<name>张三</name>
		<age>23</age>
		<num>5115</num>
	</user>
	<user id="2">
		<name>李四</name>
		<age>18</age>
		<num>5116</num>
	</user>
	<user id="3">
		<name>娜娜</name>
		<age>34</age>
		<num>523</num>
	</user>
</users>

User.java

package com.hl.chapter1;

public class User {

	private String id;
	private String name;
	private String age;
	private String num;
	
	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 getNum() {
		return num;
	}
	public void setNum(String num) {
		this.num = num;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", age=" + age + ", num=" + num + "]";
	}
	
}

 

测试

SAXParseXML.java

package com.hl.chapter1;

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 SAXParseXML {
	
	public static void main(String[] args) throws Exception {
		SAXParserFactory factory = SAXParserFactory.newInstance();
		SAXParser saxParser = factory.newSAXParser();
		SAXParseHandler saxParseHandler = new SAXParseHandler();
		saxParser.parse("src/com/hl/chapter1/user.xml", saxParseHandler);
		List<User> users = saxParseHandler.getUser();
		for(User user:users){
			System.out.println(user);
		}
	}

}

 

相关标签: SAX解析xml