demonking
demonking

Reputation: 2654

SAX Parser and XAPI xml

i want to parse a XAPI xml with the SAXParser. But now i have a problem.

First a snippet of the 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"/>

And a snippet of my SAXParser code:

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);
  }

}

The Latitude and Longtitude aren't the problem , but the Tag "tag".

How can i check for the "k" and get the Value of v ?

Anyone an idea? :)

Upvotes: 1

Views: 164

Answers (1)

nicholas.hauschild
nicholas.hauschild

Reputation: 42849

The values you are interested in looking at are xml attributes, and will be represented in the startElement(...) method by the Attributes argument passed in.

What you need to do is very similar to what you have done for the node element.

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);
        }
    }
}

Upvotes: 3

Related Questions