使用 SAX 解析 KML 文件时出现问题

发布于 2024-10-09 16:44:24 字数 5219 浏览 4 评论 0原文

我正在使用 SAX 解析 KML,并且只有 Placemark 标记的前 2 个属性被正确读取(名称和描述)。其他 2 个(样式、坐标)为空或 null。我的测试 KML 文件如下所示:

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.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>

我的解析器类:

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 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.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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸠书 2024-10-16 16:44:24

我在这段代码中发现了错误:

 else if (inStyle) {
    currentPlacemark.setStyle(s);
    inPoint=false; //WRONG

inStyle 标志应该设置为 false,而不是 inPoint。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文