import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class SaxHandler extends DefaultHandler { /* 此方法有三个参数 arg0是传回来的字符数组,其包含元素内容 arg1和arg2分别是数组的开始位置和结束位置 */ @Override public void characters(char[] arg0, int arg1, int arg2) throws SAXException { String content = new String(arg0, arg1, arg2); System.out.println(content); super.characters(arg0, arg1, arg2); } @Override public void endDocument() throws SAXException { System.out.println("\n…………结束解析文档…………"); super.endDocument(); } /* arg0是名称空间 arg1是包含名称空间的标签,如果没有名称空间,则为空 arg2是不包含名称空间的标签 */ @Override public void endElement(String arg0, String arg1, String arg2) throws SAXException { System.out.println("结束解析元素 " + arg2); super.endElement(arg0, arg1, arg2); } @Override public void startDocument() throws SAXException { System.out.println("…………开始解析文档…………\n"); super.startDocument(); } /*arg0是名称空间 arg1是包含名称空间的标签,如果没有名称空间,则为空 arg2是不包含名称空间的标签 arg3很明显是属性的集合 */ @Override public void startElement(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException { System.out.println("开始解析元素 " + arg2); if (arg3 != null) { for (int i = 0; i < arg3.getLength(); i++) { // getQName()是获取属性名称, System.out.print(arg3.getQName(i) + "=\"" + arg3.getValue(i) + "\""); } } System.out.print(arg2 + ":"); super.startElement(arg0, arg1, arg2, arg3); } } import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class SaxHandler extends DefaultHandler { private HashMap<String, String> map = null; private List<HashMap<String, String>> list = null; /** * 正在解析的元素的标签 */ private String currentTag = null; /** * 正在解析的元素的值 */ private String currentValue = null; private String nodeName = null; public List<HashMap<String, String>> getList(){ return list; } public SaxHandler(String nodeName) { this.nodeName = nodeName; } @Override public void startDocument() throws SAXException { // TODO 当读到一个开始标签的时候,会触发这个方法 list = new ArrayList<HashMap<String,String>>(); } @Override public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException { // TODO 当遇到文档的开头的时候,调用这个方法 if(name.equals(nodeName)){ 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 = name; } @Override public void characters(char[] ch, int start, int length) throws SAXException { // TODO 这个方法用来处理在XML文件中读到的内容 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 name) throws SAXException { // TODO 在遇到结束标签的时候,调用这个方法 if(name.equals(nodeName)){ list.add(map); map = null; } super.endElement(uri, localName, name); } }