parserxml.java~1~
来自「100多M的J2EE培训内容」· JAVA~1~ 代码 · 共 63 行
JAVA~1~
63 行
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; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?