map = new HashMap<String, String>();
if(attributes!=null && map!=null){
for(int i=0;i<attributes.getLength();i++){
map.put(attributes.getQName(i), attributes.getValue(i));
}
}
}
currentTag = qName;
}
//当获取元素的值时候触发调用
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if(currentTag!=null && map!=null){
currentValue = new String(ch, start, length);
if(currentValue!=null && !currentValue.trim().equals("") && !currentValue.trim().equals("\n")){
map.put(currentTag, currentValue);
}
}
//获取完毕重置值
currentTag = null;
currentValue = null;
}
//遇到结束标记的时候调用这个方法
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if(qName.equals(nodeName)){
list.add(map);
map = null;
}
}
}
SaxService.java
package com.sax.service;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
import com.sax.handler.MyHandler;
public class SaxService {
public SaxService() {
}