ParserXml.java~1~
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:2k
源码类别:
Java编程
开发平台:
Java
- package northwindcase;
- import java.io.IOException;
- import org.xml.sax.*;
- import org.xml.sax.helpers.*;
- import javax.xml.parsers.*;
- import java.util.*;
- public class ParserXml extends DefaultHandler {
- private String currentName;
- private String currentValue;
- private String fileName;
- private Properties props;
- public ParserXml(String fileName) {
- props = new Properties();
- this.fileName = fileName;
- parse();
- }
- public Properties getProps() {
- return props;
- }
- private void parse() {
- try {
- SAXParserFactory parserFactory = SAXParserFactory.newInstance();
- parserFactory.setValidating(false);
- parserFactory.setNamespaceAware(false);
- SAXParser parser = parserFactory.newSAXParser();
- parser.parse(fileName,this);
- parserFactory = null;
- parser = null;
- }
- catch(IOException ex) {
- ex.printStackTrace();
- }
- catch(SAXException ex) {
- ex.printStackTrace();
- }
- catch(ParserConfigurationException ex) {
- ex.printStackTrace();
- }
- catch(FactoryConfigurationError ex) {
- ex.printStackTrace();
- }
- }
- public void characters(char[] ch, int start, int length) throws SAXException {
- currentValue = new String(ch,start,length).trim();
- }
- public void endElement(String uri, String localName, String qName) throws SAXException {
- if (currentValue.equals("")) return;
- props.put(currentName,currentValue);
- }
- public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
- currentName = qName;
- }
- }