SAX Parser and XAPI xml



我想用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 的值?

有人知道吗?:)

您有兴趣查看的值是 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);
        }
    }
}

最新更新