Java基础之XML(SAX解析)

版权声明:学习之路,任重道远 https://blog.csdn.net/weixin_43359405/article/details/83930557

代码如下:

package com.briup.SAXXML;

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

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

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

public class SAXXmlTest {
	// /保存数据的list
	private List<Phone> list;

	// /新建一个解析工具,也就是一个解析类
	class MyParser extends DefaultHandler {
		private List<Phone> list;
		private Phone phone;
		private String tagName;

		public MyParser(List<Phone> list) {
			this.list = list;
		}

		@Override
		public void startDocument() throws SAXException {
			// /无须做任何处理
		}

		@Override
		public void endDocument() throws SAXException {
			// /输出信息如下
			System.out.println("数目:" + list.size());
			for (int i = 0; i < list.size(); i++) {
				System.out.println(list.get(i).toString());
			}
		}

		@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			if (qName.equals("list")) {
				phone = new Phone();
				phone.setTest(attributes.getValue("test"));
			} else {
				tagName = qName;
			}
		}

		@Override
		public void endElement(String uri, String localName, String qName)
				throws SAXException {
			if ("list".equals(qName)) {
				list.add(phone);
			}
			// /防止读取到无用字符,对上一次的属性进行替换或者强制转换错误异常
			tagName = null;
		}

		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
			String str = new String(ch, start, length);
			if ("id".equals(tagName))
				phone.setId(Integer.parseInt(str));
			else if ("city".equals(tagName))
				phone.setCity(str);
			else if ("cardtype".equals(tagName))
				phone.setCardtype(str);
			else if ("code".equals(tagName))
				phone.setCode(Integer.parseInt(str));
			else if ("num".equals(tagName))
				phone.setNum(Integer.parseInt(str));
		}
	}

	public SAXXmlTest() {
		list = new ArrayList<Phone>();
	}

	public void createPhoneList(String address) {
		try {
			// /新建一个自定义的sax解析类
			MyParser myparse = new MyParser(list);
			// /新建一个解析工厂类
			SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
			// /新建一个解析类
			SAXParser saxparser = saxParserFactory.newSAXParser();
			// /新建一个解析对象xml文件
			File file = new File(address);
			// /开始解析获取数据
			saxparser.parse(file, myparse);
			System.out.println("建造完成");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		SAXXmlTest sax = new SAXXmlTest();
		String address = "src/com/briup/SAXXML/phone.xml";
		sax.createPhoneList(address);
	}
}

XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<dataroot>
	<list test = "t1">
		<id>1</id>
		<num>1300000</num>
		<code>531</code>
		<city>山东济南</city>
		<cardtype>联通</cardtype>
	</list>
	<list test = "t2">
		<id>2</id>
		<num>1300001</num>
		<code>519</code>
		<city>江苏常州</city>
		<cardtype>联通</cardtype>
	</list>
</dataroot>

PhoneJava文件:

package com.briup.SAXXML;

public class Phone {
	private String test;
	private int id;
	private int num;
	private int code;
	private String city;
	private String cardtype;

	public String getTest() {
		return test;
	}

	public void setTest(String test) {
		this.test = test;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getCardtype() {
		return cardtype;
	}

	public void setCardtype(String cardtype) {
		this.cardtype = cardtype;
	}

	@Override
	public String toString() {
		return "Phone [test=" + test + ", id=" + id + ", num=" + num
				+ ", code=" + code + ", city=" + city + ", cardtype="
				+ cardtype + "]";
	}

	public Phone(String test, int id, int num, int code, String city,
			String cardtype) {
		this.test = test;
		this.id = id;
		this.num = num;
		this.code = code;
		this.city = city;
		this.cardtype = cardtype;
	}

	public Phone() {

	}

}

猜你喜欢

转载自blog.csdn.net/weixin_43359405/article/details/83930557