使用SAX解析xml文件

通常解析xml文件我们主要用到的时候利用第三方的jar包如:jdom,dom4j来解析xml文件,但是这里使用的是jdk自带的解析类库,使用jdk自带的类库SAX方式解析的话,解析效率会比较高:

 

 当我们使用jdk自带的解析类库的话,我们必须要继承DefaultHandler.java的类库:

xml事件处理类:

/**
 * 
 */
package com.sandy.xml.util;

import java.util.HashMap;
import java.util.Map;

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

/**
 * @author sandy
 * @since
 * 
 */
public class XMLHandler extends DefaultHandler {
	private Map<String, Object> props = null;
	private StringBuffer currentValue = null;

	public XMLHandler() {
		this.props = new HashMap<String, Object>();
		this.currentValue = new StringBuffer();
	}

	/**
	 * @return the props
	 */
	public Map<String, Object> getProps() {
		return props;
	}
	/**
	 * @param props
	 *            the props to set
	 */
	public void setProps(Map<String, Object> props) {
		this.props = props;
	}

	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		int length=attributes.getLength();
		if(length!=0){
			for(int i=0;i<length;i++){
				System.out.println(attributes.getQName(i)+"="+attributes.getValue(i));
			}
		}
		currentValue.delete(0, currentValue.length());
		System.out.println("(startElement:qName)==="+qName);
		// this.currentName = qName;
	}
	/**
	 * @author sandy
	 * 获取标签内容
	 */
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		currentValue.append(ch, start, length);
		System.out.println("characters+currentValue:" + currentValue);
	}
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if (!qName.equalsIgnoreCase("root"))
			props.put(qName, currentValue.toString().trim());
		 System.out.println("(endElement:qName)==="+qName);
	}
}

 

解析类:

/**
 * 
 */
package com.sandy.xml.parse;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Set;

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

import org.xml.sax.SAXException;

import com.sandy.xml.util.XMLHandler;

/**
 * @author sandy
 *
 */
public class ParseXML {

	private Map<String, Object> props=null;
	private void parse(InputStream in) throws IOException {
		XMLHandler handler = new XMLHandler();
		SAXParserFactory factory = SAXParserFactory.newInstance();
		SAXParser parser = null;
		try {
			try {
				parser = factory.newSAXParser();
				parser.parse(in, handler);
				props = handler.getProps();
			} catch (ParserConfigurationException e) {
				System.out.println("ParserConfigurationException");
				e.printStackTrace();
			} catch (SAXException e) {
				System.out.println("SAXException");
				e.printStackTrace();
			}
		} finally {
			factory = null;
			parser = null;
			handler = null;
		}
	}
	/**
	 * @return the props
	 */
	public Map<String, Object> getProps() {
		return props;
	}
	public static void main(String[] args) {

		ParseXML xml = new ParseXML();
		String xmlString="<ROOT type='request'>"
		+"<stdmsgtype>0100</stdmsgtype>"
		+"<std400trcd>201101</std400trcd>"
		+"<stdprocode>201101</stdprocode>"
		+"<std400aqid>N</std400aqid>"
		+"<std400tlno>0279</std400tlno>"
		+"<stdlocdate>20100423</stdlocdate>"
		+"<stdloctime>110541</stdloctime>"
		+"<std400autl></std400autl>"
		+"<stdauthid></stdauthid>"
		+"<stdadddtap></stdadddtap>"
		+"<stdibsdate></stdibsdate>"
		+"<stdrefnum></stdrefnum>"
		+"<stdsetdate></stdsetdate>"
		+"<std400trno></std400trno>"
		+"<std400mgid></std400mgid>"
		+"<std400acur></std400acur>"
		+"<stdmercno>010100099970014</stdmercno>"
		+"<M_CODE>000000000000000025</M_CODE>"
		+"<M_CODETYPE>0</M_CODETYPE>"
	+"</ROOT>";
		try {
			xml.parse(new ByteArrayInputStream(xmlString.getBytes()));
			Map<String,Object> m=xml.getProps();
			Set<Map.Entry<String, Object>> s=m.entrySet();
			for(Object o:s){
				System.out.println(o);
			}
		} catch (IOException e) {
			System.out.println("解析xml出错!");
			e.printStackTrace();
		}
	}
}

 

说明:这种解析方式是基于事件驱动的,运用的是基于观察者的设计模式.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值