`

SAX解析XML

    博客分类:
  • java
阅读更多

public class XMLParse {
	// 定义一个Properties 用来存放标签值
	private Properties props;

	public Properties getProps() {
		return this.props;
	}

	public void parse(String filename) throws Exception {
		// 将解析器对象化
		try {
			ConfigParser handler = new ConfigParser();
			// 获取SAX工厂对象
			SAXParserFactory factory = SAXParserFactory.newInstance();
			factory.setNamespaceAware(false);
			factory.setValidating(false);
			// 获取SAX解析
			SAXParser parser = factory.newSAXParser();

			// ///////////////////////////////////////////////////////////////////////////
			// 对字符串解析:
			InputSource is = new InputSource();
			StringReader xmlStr = new StringReader(filename);
			is.setCharacterStream(xmlStr);
			parser.parse(is, handler);
			// //////////////////////////////////////////////////////////////////////////

			// //////////////////////////////////////////////////////////////////////////
			// 对文件解析:
			// URL confURL = getClass().getResource(filename);
			// if (confURL == null)
			// System.out.println("error");

			// 将解析器和解析对象xml联系起来,开始解析
			// parser.parse(confURL.toString(), handler);
			// ///////////////////////////////////////////////////////////////////////
			props = handler.getProps();
		} catch (Exception e) {
			System.out.println(e.toString());
		}
	}
}

class ConfigParser extends DefaultHandler {
	// //定义一个Properties 用来存放 dbhost dbuser dbpassword的值
	private Properties props;

	private String currentName;
	private StringBuffer currentValue = new StringBuffer();

	// 构建器初始化props
	public ConfigParser() {
		this.props = new Properties();
	}

	public Properties getProps() {
		return this.props;
	}

	// 定义开始解析元素的方法. 这里是将<xxx>中的名称xxx提取出来.
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		currentValue.delete(0, currentValue.length());
		this.currentName = qName;
	}

	// 这里是将<xxx></xxx>之间的值加入到currentValue
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		currentValue.append(ch, start, length);
	}

	// 在遇到</xxx>结束后,将之前的名称和值一一对应保存在props中
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		props.put(qName.toLowerCase(), currentValue.toString().trim());
	}
}




	
	private final String smsReplyXmlTagId = "id";
	
	private final String smsReplyXmlTagMessage = "message";
	
	private final String smsReplyXmlTagReplyTime = "time";
	
	private final String smsReplyXmlTagPhone = "src";

        public Map<String , String> smsReplyFilter(String xmlStr) throws SystemException , Exception{
        Map<String , String> replyInfoMap = new HashMap<String , String>();
        
        XMLParse xp = new XMLParse();
        xp.parse(xmlStr);
        String id = getObject(xp, smsReplyXmlTagId);
        String message = getObject(xp , smsReplyXmlTagMessage);
        String replyTime = getObject(xp, smsReplyXmlTagReplyTime);
        String phone = getObject(xp, smsReplyXmlTagPhone);
        
        replyInfoMap.put("id" , id);
        replyInfoMap.put("message", message);
        replyInfoMap.put("replyTime", replyTime);
        replyInfoMap.put("phone", phone);
        
        return replyInfoMap;
    }

        private String getObject(XMLParse xp , String keyname) throws SystemException {
		if (keyname == null){
			throw new SystemException("解析XML,XML标签名为空!");
		}
		if (xp == null){
			throw new SystemException("解析XML,XML对象为空!");
		}
		return xp.getProps().getProperty(keyname);
	}


这些代码主要是传入字符串,进行解析,而我需要的是一些简单的解析方式,没有对XML的层次有要求,因此对XML的层次没有进行测试
而用流读文件,没有进行测试
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics