0
我想解析一些处理位置的XML。这是一个XML示例。如何跳过XML标签?
<location>
<city lat="51.5078" lng="-0.128" displayName="London">
<country displayName="UK"/>
</city>
<metroArea lat="51.5078" lng="-0.128" displayName="London" id="24426">
<country displayName="UK"/>
</metroArea>
</location>
<location>
<city lat="42.9833" lng="-81.25" displayName="London">
<country displayName="Canada"/>
<state displayName="ON"/>
</city>
<metroArea lat="42.9833" lng="-81.25" displayName="London" id="27374">
<country displayName="Canada"/>
<state displayName="ON"/>
</metroArea>
</location>
在上面的XML伦敦,英国没有一个州,但伦敦,加拿大。分析我的结果时
- 伦敦,
- 英国
- 伦敦,KY
- 加拿大
如何跳过 “状态” 标签,以达到以下效果?
- 伦敦
- 英国
- 伦敦,
- 加拿大
这是我使用的Java代码。
private void addCitiesToHashmap(List<HashMap<String, String>> menuItems, String xml)
{
XMLParser parser = new XMLParser();
Document doc = parser.getDomElement(xml);
NodeList nl = doc.getElementsByTagName(KEY_METRO_AREA);
for (int i = 0; i < nl.getLength(); i++)
{
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
map.put(KEY_ID, e.getAttribute(KEY_ID));
NodeList nl3 = doc.getElementsByTagName(KEY_CITY);
{
Element g = (Element) nl3.item(i);
map.put(KEY_DISPLAY_NAME , g.getAttribute(KEY_DISPLAY_NAME));
}
NodeList nl4 = doc.getElementsByTagName(KEY_COUNTRY);
{
Element h = (Element) nl4.item(i);
map.put(KEY_DISPLAY_NAME_4 + "3" , h.getAttribute(KEY_DISPLAY_NAME_4));
}
NodeList nl2 = doc.getElementsByTagName(KEY_STATE);
{
Element f = (Element) nl2.item(i);
if (f != null)
map.put(KEY_DISPLAY_NAME_2 + "2" , f.getAttribute(KEY_DISPLAY_NAME_2));
menuItems.add(map);
}
}
肯塔基州从哪里来的样本XML? – wchargin 2013-05-10 21:22:15
这些只是13个XML位置中的前两个。肯塔基州的伦敦肯定有;伦敦,俄亥俄州;等肯塔基州是下一个位置。 – user2305700 2013-05-10 21:31:10
请澄清您的需求的业务逻辑:您是否需要在第一行或每一行中“跳过”一个状态? Rgds, – 2013-05-10 21:38:13