1

SAXParser を使用して XAPI xml を解析したいと考えています。しかし今、私には問題があります。

まず、xml のスニペット:

<node id="24924135" lat="49.8800274" lon="8.6453740" version="12" timestamp="2012-05-25T15:13:47Z" changeset="11699394" uid="61927" user="AlexPleiner">
<tag k="addr:city" v="Darmstadt"/>
<tag k="addr:housenumber" v="41"/>
<tag k="addr:postcode" v="64293"/>
<tag k="addr:street" v="Kahlertstraße"/>
<tag k="amenity" v="pub"/>
<tag k="name" v="Kneipe 41"/>
<tag k="note" v="DaLUG Meeting (4st Friday of month 19:30)"/>
<tag k="smoking" v="no"/>
<tag k="website" v="http://www.kneipe41.de/"/>
<tag k="wheelchair" v="limited"/>

そして、私の SAXParser コードのスニペット:

public void startElement(String uri, String localName, String qName,
        Attributes atts) throws SAXException {
  if (localName.equals("node")) {
    // Neue Person erzeugen
    poi = new POIS();

    poi.setLat(Float.parseFloat(atts.getValue("lat")));
    poi.setLon(Float.parseFloat(atts.getValue("lon")));
  }
}

public void endElement(String uri, String localName, String qName) throws SAXException {


  poi.setHouseNo(currentValue);


  if (localName.equals("addr:street")) {
    poi.setStreet(currentValue);
  }

  if (localName.equals("amenity")) {
    poi.setType(currentValue);
  }

}

緯度と経度は問題ではありませんが、タグ「タグ」です。

「k」をチェックして v の値を取得するにはどうすればよいですか?

誰でもアイデアはありますか?:)

4

1 に答える 1

3

調べたい値はxml属性であり、渡された引数startElement(...)によってメソッドで表されます。Attributes

あなたがする必要があることはあなたがnode要素のためにしたことと非常に似ています。

public void startElement(String uri, String localName, String qName,
          Attributes atts) throws SAXException 
{
    //your other node code        

    if(localname.equals("tag")) {
        String k = atts.getValue("k");
        if(iAmInterestedInThisK(k)) {
            String v = atts.getValue("v");
            doSomethingWithThisV(v);
        }
    }
}
于 2012-06-11T17:49:16.047 に答える