我想用 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);
}
}
}
关于java - SAX 解析器和 XAPI xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10984954/