SaxParserHandler Class
The SaxParserHandler class is used by program in order to parse and store all well-formed KML data found in the document. This class inherits from org.xml.sax.helpers.DefaultHandler and overwrites five methods that are required to parse the XML.
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 | public class NavigationSaxHandler extends DefaultHandler {
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// DO STUFF
super.endElement(uri, localName, qName);
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// DO STUFF
super.startElement(uri, localName, qName, attributes);
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
//DO STUFF
super.endElement(uri, localName, qName);
}
/**
* Es llamado al comenzar el documento
*/
@Override
public void endDocument() throws SAXException {
super.endDocument();
}
/**
* Es llamado al finalizar el documento
*/
@Override
public void startDocument() throws SAXException {
// INITIALIZATION
super.startDocument();
}
}
|
The startDocument() and endDocument() methods are called when the internal parser starts and ends to read the XML document respectively. I've used the startDocument() in order to initialize all required class objects.
The startElement() and endElement() methods operates similarly but in this time, they are called when the parser locates an XML element that begins or ends respectively.
The characters() method is called when when the parser finds anything other than an xml element. That is, it should contain the value of that element.
You can get all the information about this helper visiting the official documentation: org.xml.sax.helpers.DefaultHandler
In this case, I've used the startElement() method to implement two important task (and other stuff):
- Initilize collectionable objects like Placemarks or Styles and the buffer.
- Know when we are in a particular XML element (through static booleans)
An example:
1
2
3
4
5
6
7
8
9
10
11
12
13 | @Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
buffer = new StringBuilder();
/* initializing current Placemark object
and establish that we are in the tag placemark
trought in_placement var */
if (localName.equalsIgnoreCase("placemark")) {
this.navSet.setCurrentPlacemark(new MyPlacemark());
in_placemarktag = true;
}
// rest of the stuff...
}
|
In characters() method I collect all values in the buffer. This is the correct way to perform this operation safely:
| @Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (buffer != null)
for (int i = start; i < start + length; i++)
buffer.append(ch[i]);
super.characters(ch, start, length);
}
}
|
In endElement() I perform the following important tasks and others:
- Store the buffer value in its appropriate variable.
- Add collectionable objects like Placermaks or Styles to the collection.
- Change the value of the boolean to false in order to indicate that the XML element has finished.