Problem with parsing KML file with SAX
I am parsing KML with SAX and only first 2 attributes of Placemark tag are read correctly (name and description). The other 2 (style, coordinates) are empty or null. My test KML file looks like this:
enter code here<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.开发者_StackOverflow中文版net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>Zaseden</name>
<Style id="msn_1">
<IconStyle>
<Icon>
<href>http://maps.google.com/mapfiles/kml/paddle/1.png</href>
</Icon>
</IconStyle>
</Style>
<Style id="msn_2">
<IconStyle>
<Icon>
<href>http://brandonkopp.com/DC_Photo_Project/DC_Photo_Project_files/green_placemark.jpg</href>
</Icon>
</IconStyle>
</Style>
<Placemark>
<name>Busy car 1</name>
<description>Test car position</description>
<styleUrl>#msn_1</styleUrl>
<Point>
<coordinates>14.50316902804375,46.05190525505257,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Busy car 2</name>
<description>Test car position 2</description>
<styleUrl>#msn_1</styleUrl>
<Point>
<coordinates>14.491353,46.080227,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Free car 1</name>
<description>Test car position</description>
<styleUrl>#msn_2</styleUrl>
<Point>
<coordinates>14.529891,46.065087,0</coordinates>
</Point>
</Placemark>
My parser class:
package com.spremljaj.me;
public class Parser extends DefaultHandler{
public static List<Placemark> placemarks;
private Placemark currentPlacemark;
boolean inName;
boolean inDesc;
boolean inStyle;
boolean inCoor;
boolean inPoint;
@Override
public void startDocument() throws SAXException {
Log.d("XML","startDocument");
placemarks = new ArrayList<Placemark>();
}
@Override
public void endDocument() throws SAXException {
Log.d("XML","endDocument");
//Log.d("Velikost lista: ",Integer.toString(placemarks.size()));
Spremljaj.setList(placemarks);
}
/** Gets called on opening tags like:
* <tag>
* Can provide attribute(s), when xml was like:
* <tag attribute="attributeValue">*/
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
// TODO Auto-generated method stub
if (localName.equalsIgnoreCase("Placemark")){
this.currentPlacemark = new Placemark();
}
else if (localName.equalsIgnoreCase("name") && currentPlacemark != null){ //drugi pogoj je za primer zacetka kmlja kjer je opis stilov a je tudi tag z imenom name
Log.d("XML","nameTag");
inName=true;
}
else if (localName.equalsIgnoreCase("description")){
Log.d("XML","descTag");
inDesc=true;
}
else if (localName.equalsIgnoreCase("styleUrl")) {
inStyle=true;
Log.d("XML","styleTag");
}
else if(localName.equalsIgnoreCase("coordinates")){
inCoor=true;
Log.d("XML","coorTag");
}
}
/** Gets called on closing tags like:
* </tag> */
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
super.endElement(uri, localName, qName);
if (this.currentPlacemark != null){
if(localName.equalsIgnoreCase("Placemark")){ //ce pridemo do konca dodamo objekt v list
placemarks.add(currentPlacemark);
}
if(localName.equalsIgnoreCase("coordinates")){ //samo za debug tale if
Log.d("konecTaga","coor");
}
}
}
@Override
public void characters(char[] ch, int start, int length) // method called with the text contents in between the start and end tags of an XML document element.
throws SAXException {
// TODO Auto-generated method stub
super.characters(ch, start, length);
String s = (new String(ch).substring(start, start + length));
if(inName) {
//Log.d("textSize",Integer.toString(s.length()));
currentPlacemark.setName(s);
inName=false;
//Log.d("XML","Prebran tekst "+s);
}
else if (inDesc) {
currentPlacemark.setDescription(s);
inDesc=false;
}
else if (inStyle) {
currentPlacemark.setStyle(s);
inPoint=false;
//Log.d("XML","Prebran tekst v stilu "+s); TLE LAHK DA JE PROBLEM
}
else if (inCoor) {
//Log.d("XML","Prebran tekst "+s);
currentPlacemark.setCoordinates(s);
inCoor=false;
Log.d("XML","dolzina koor "+Integer.toString(s.length()));
}
//Log.d("XML","Prebran tekst "+s);
}
}
I have no idea what is going wrong, because i am doing the same thing for all four attributes of Placemark.
I found mistake in this code:
else if (inStyle) {
currentPlacemark.setStyle(s);
inPoint=false; //WRONG
inStyle flag should be set to false here instead of inPoint.
精彩评论