MyDefaultHandler.java
资源名称:xml001.rar [点击查看]
上传用户:zhaokuo188
上传日期:2022-05-16
资源大小:989k
文件大小:1k
源码类别:
xml/soap/webservice
开发平台:
Java
- package com.waytojob.xml;
- import java.util.HashMap;
- import java.util.LinkedHashMap;
- import org.xml.sax.Attributes;
- import org.xml.sax.SAXException;
- import org.xml.sax.helpers.DefaultHandler;
- public class MyDefaultHandler extends DefaultHandler {
- private String key;
- private StringBuilder value = new StringBuilder();
- private HashMap<String, String> hm = new LinkedHashMap<String, String>();
- public HashMap<String, String> getHm() {
- return hm;
- }
- @Override
- public void characters(char[] ch, int start, int length)
- throws SAXException {
- super.characters(ch, start, length);
- value.append(ch, start, length);
- }
- @Override
- public void endElement(String uri, String localName, String name)
- throws SAXException {
- super.endElement(uri, localName, name);
- hm.put(key, value.toString().trim());
- }
- @Override
- public void startElement(String uri, String localName, String name,
- Attributes attributes) throws SAXException {
- super.startElement(uri, localName, name, attributes);
- key = name;
- value.delete(0, value.length());
- }
- }