我无法解析xml文件。 我有以下活动代码:
Document doc = Jsoup.parse(sb.toString(), "", Parser.xmlParser());
for (Element e : doc.select("Placemark")) {
Log.d(TAG, "name is " + e.attr("name")+ "cordinates is" + e.attr("coordinates"));
我有这个xml示例:
<Placemark>
<name>international route for military only</name>
<styleUrl>#m_ylw-pushpin0</styleUrl>
<LineString>
<tessellate>1</tessellate>
<coordinates>
34.9524993959688,32.7730878314906,0 34.95166998854646,32.77308993256781,0 34.95125522754987,32.77309097997785,0 34.95070215058794,32.77309237599226,0 34.94918083131482,32.77309620394223,0 34.94752038951339,32.77309850484357,0 34.94655149233169,32.77309830859218,0 34.94572098730102,32.77309813906572,0 34.94502892287641,32.77309798764293,0 34.94419838488641,32.77309782117877,0 34.94309104377032,32.77309757681172,0 34.94226051634391,32.77309738973906,0 34.94115313434187,32.77309712978963,0 34.94018416814745,32.77309689649781,0 34.93935361177677,32.77309669003321,0 34.93866147471769,32.77309651419306,0 34.93769247332618,32.77309626314753,0 34.93658505149507,32.77309586239551,0 34.93533920626601,32.77309542577984,0 34.93450864230719,32.77309510022808,0 34.93340122016876,32.77309465949123,0 34.93160165271385,32.77309392217991,0 34.92883324477039,32.7728599214421,0 34.92385060482191,32.77169369268832,0 34.91540783742491,32.76947803887981,0 34.90793372651729,32.7673788666683,0 34.90322739422959,32.76621216804099,0 34.89893646854004,32.76481273889556,0 34.89533713275235,32.76387923817537,0 34.89339945743092,32.76306307317424,0 34.89215360021323,32.76271299419985,0
</coordinates>
</LineString>
</Placemark>
我在打印文档时正确解析。但是当我试图通过访问其属性字段来获取地标的名称和坐标时,它会给我一个空字符串。
当我使用doc.select("Placemark")
时,它只会占用标签内的字符串并忽略标签本身。
答案 0 :(得分:1)
使用e.select("name").text()
for (Element e : doc.select("Placemark")) {
Log.d(TAG, "name is " + e.select("name").text()+ "cordinates is" + e.select("coordinates").text());
}