-
Notifications
You must be signed in to change notification settings - Fork 3
/
SAXParser.java
78 lines (64 loc) · 1.99 KB
/
SAXParser.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
/*
* Reference:
* http://docs.oracle.com/javase/tutorial/jaxp/sax/parsing.html
*/
public class SAXParser extends DefaultHandler {
public List<String> nodeValues = null;
boolean assignmentNode = false;
char vals[] = new char[100];
int i=0;
public static void main(String [] args) throws ParserConfigurationException, SAXException, IOException {
if (args.length < 1) {
System.out.println("Usage:");
System.out.println("java SAXParser <XML File>");
System.exit(0);
}
String fileName = args[0];
SAXParserFactory spf = SAXParserFactory.newInstance();
javax.xml.parsers.SAXParser saxParser = spf.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setContentHandler(new SAXParser());
xmlReader.parse(fileName);
}
public void startElement(String namespaceURI,
String localName,
String qName,
Attributes atts) throws SAXException {
if (qName.equalsIgnoreCase("assignment")) {
assignmentNode = true;
}
}
public void endElement(String namespaceURI,
String localName,
String qName) throws SAXException {
if (qName.equalsIgnoreCase("assignment")) {
assignmentNode = false;
vals[i] = '\0';
i=0;
String assignmentName = new String(vals);
vals = new char[100];
System.out.println("Node value:" + assignmentName);
}
}
public void characters(char[] ch, int start, int length) throws SAXException {
if (assignmentNode) {
for(int j=start; j<start+length; j++, i++) {
vals[i] = ch[j];
}
}
}
public void startDocument() throws SAXException {
nodeValues = new ArrayList<String>();
}
public void endDocument() throws SAXException {
}
}