1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| public class SAXParserHandler extends DefaultHandler {
private String value = null;
@Override public void startDocument() throws SAXException { super.startDocument();
System.out.println("开始解析文档"); }
@Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { super.startElement(uri, localName, qName, attributes);
int end = attributes.getLength(); if (end > 0) { for (int i = 0; i < end; i++) { System.out.println(attributes.getQName(i)); System.out.println(attributes.getValue(i)); } }
}
@Override public void characters(char[] ch, int start, int length) throws SAXException { super.characters(ch, start, length);
value = new String(ch, start, length); }
@Override public void endElement(String uri, String localName, String qName) throws SAXException { super.endElement(uri, localName, qName);
if (qName.equals("username")) { System.out.println(value); } value = null; }
@Override public void endDocument() throws SAXException { super.endDocument();
System.out.println("结束解析文档"); } }
|