-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex8-3.java
54 lines (46 loc) · 1.75 KB
/
ex8-3.java
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
import org.xml.sax.XMLReader;
import org.xml.sax.SAXException;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
public class SaxHandlerClass3 extends DefaultHandler
{
public static void main(String[] args) throws Exception
{
System.out.println("start...");
SaxHandlerClass3 saxhandler = new SaxHandlerClass3();
saxhandler.read(args[0]);
}
public void read(String fileName) throws Exception
{
XMLReader readerObj = XMLReaderFactory.createXMLReader(
"org.apache.xerces.parsers.SAXParser");
readerObj.setContentHandler(this);
readerObj.parse(fileName);
}
public void startDocument() throws SAXException
{ System.out.println("parsing start ... "); }
public void endDocument() throws SAXException
{ System.out.println("parsing end."); }
public void startElement(String uri, String localName,
String fullName, Attributes atts) throws SAXException
{
System.out.println("Element is " + fullName);
for(int i=0; i<atts.getLength(); ++i) {
String name = atts.getLocalName(i);
String value = atts.getValue(i);
System.out.println("Element(" + localName+ ")has " + "attr(" + name + "), " + "value("+value+")");
}
strbuf.setLength(0);
}
StringBuffer strbuf = new StringBuffer();
public void characters(char[] chars, int start, int len) {
strbuf.append(chars, start, len);
}
public void endElement(String uri, String localName, String fullname){
if(strbuf.length() >0) {
System.out.println(localName + "has " + strbuf);
strbuf.setLength(0);
}
}
}