2)SAX解析

<?xml version="1.0" encoding="UTF-8"?>
<exam>
	<student cid="440440199002152413" exid="1359070701">
		<name>ZhangSan</name>
		<gread>83</gread>
		<location>GuangDong.ZhuHai</location>
	</student>
	<student cid="440102198902152413" exid="1359070702">
		<name>LiSi</name>
		<gread>79</gread>
		<location>GuangDong.GuangZhou</location>
	</student>
</exam>

—————————————————————————————————————————————————————————————————————————————

package cn.zen.sax;

import java.util.List;

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

import org.junit.Test;
import org.xml.sax.XMLReader;

import cn.zen.entity.Student;

public class SaxOperXml {
	public static void main(String[] args) {

	}

	@Test
	public void getXmlData() throws Exception {
		// SAX 的解析器工厂
		SAXParserFactory spf = SAXParserFactory.newInstance();
		// 创建 SAX 的解析器
		SAXParser sp = spf.newSAXParser();
		// 创建xml阅读器
		XMLReader xr = sp.getXMLReader();
		// 设置事件处理器
		xr.setContentHandler(new doContentHandler());

		xr.parse("src/exam.xml");
	}

	@Test
	public void getAppointXmlData() throws Exception {
		// SAX 的解析器工厂
		SAXParserFactory spf = SAXParserFactory.newInstance();
		// 创建 SAX 的解析器
		SAXParser sp = spf.newSAXParser();
		// 创建xml阅读器
		XMLReader xr = sp.getXMLReader();
		// 设置事件处理器
		xr.setContentHandler(new doAppointHander());

		xr.parse("src/exam.xml");
	}

	@Test
	public void getBeanListXmlData() throws Exception {
		// SAX 的解析器工厂
		SAXParserFactory spf = SAXParserFactory.newInstance();
		// 创建 SAX 的解析器
		SAXParser sp = spf.newSAXParser();
		// 创建xml阅读器
		XMLReader xr = sp.getXMLReader();

		doGetListHander dh = new doGetListHander();
		// 设置事件处理器
		xr.setContentHandler(dh);

		xr.parse("src/exam.xml");

		List<Student> stuList = dh.getBeanList();

		for (int i = 0; i < stuList.size(); i++) {
			Student stu = stuList.get(i);

			System.out.println("---------------------------------");
			System.out.println("cid:" + stu.get_cid());
			System.out.println("exid:" + stu.get_exid());
			System.out.println("name:" + stu.get_name());
			System.out.println("gread:" + stu.get_gread() + "");
			System.out.println("location:" + stu.get_location());
		}
	}
}
—————————————————————————————————————————————————————————————————————————————

package cn.zen.sax;

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

public class doContentHandler extends DefaultHandler {

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		StringBuffer sb = new StringBuffer();
		sb.append("<" + qName);
		for (int i = 0; attributes != null && i < attributes.getLength(); i++) {
			sb.append(" " + attributes.getQName(i) + "=" + attributes.getValue(i));
		}
		sb.append(">");
		System.out.println(sb.toString());
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		System.out.println(new String(ch, start, length));
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		System.out.println("</" + qName + ">");
	}
}
—————————————————————————————————————————————————————————————————————————————

package cn.zen.sax;

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

public class doAppointHander extends DefaultHandler {
	private String currentTag = null;
	private int iCurrIndex = 0;// 当前读取的是第几个节点
	private int iGetIndex = 2;// 读取的是哪个节点

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if ("gread".equals(qName)) {
			currentTag = qName;
			iCurrIndex++;
		}
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		if ("gread".equals(currentTag) && iGetIndex == iCurrIndex) {
			System.out.println(new String(ch, start, length));
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		currentTag = null;
	}

}
—————————————————————————————————————————————————————————————————————————————

package cn.zen.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;

import cn.zen.entity.Student;

public class doGetListHander extends DefaultHandler {
	private List<Student> stuList = null;
	private Student stu = null;
	private String currentTag = null;

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if ("exam".equals(qName)) {
			stuList = new ArrayList<Student>();
		}
		// 当开始Node的NodeName='student'时;
		if ("student".equals(qName)) {
			stu = new Student();
			for (int i = 0; attributes != null && i < attributes.getLength(); i++) {
				String qNodeTag = attributes.getQName(i);
				String qNodeVal = attributes.getValue(i);
				if ("cid".equals(qNodeTag)) {
					stu.set_cid(qNodeVal);
				}
				if ("exid".equals(qNodeTag)) {
					stu.set_exid(qNodeVal);
				}
			}
			currentTag = qName;
		}
		// 当开始Node的NodeName='name'时;
		if ("name".equals(qName)) {
			currentTag = "name";
		}
		// 当开始Node的NodeName='gread'时;
		if ("gread".equals(qName)) {
			currentTag = "gread";
		}
		// 当开始Node的NodeName='location'时;
		if ("location".equals(qName)) {
			currentTag = "location";
		}
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		if ("name".equals(currentTag)) {
			stu.set_name(new String(ch, start, length));
		}
		if ("gread".equals(currentTag)) {
			double gread = Double.parseDouble(new String(ch, start, length));
			stu.set_gread(gread);
		}
		if ("location".equals(currentTag)) {
			stu.set_location(new String(ch, start, length));
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if ("student".equals(qName)) {
			stuList.add(stu);
			stu = null;
		}
		currentTag = null;
	}

	public List<Student> getBeanList() {
		return stuList;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值