在Android中使用DOM解析XML [英] Parse XML using DOM in android
本文介绍了在Android中使用DOM解析XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想根据选择的用户解析XML和显示列表
Hi i want to parse XML and display list based on selection of user
我的XML是这样看
下面是我的code
try {
XMLParser parser = new XMLParser();
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList n1 = doc.getElementsByTagName("company");
// looping through all item nodes <item>
for (int i = 0; i < n1.getLength(); i++) {
// creating new HashMap
Element e = (Element) n1.item(i);
System.out.println("name node "+parser.getValue(e, "name"));
}
通过这样的方式,我越来越喜欢
by this way i am getting the output like
Company ABC
Company XYZ
的企业名单
但
我会写code像
but
i would write code like
NodeList n1 = doc.getElementsByTagName("province");
// looping through all item nodes <item>
for (int i = 0; i < n1.getLength(); i++) {
// creating new HashMap
Element e = (Element) n1.item(i);
System.out.println("name node "+parser.getValue(e, "name"));
}
我收到省名单
i am getting list of province name
Alberta
Ontario
New York
Florida
但它应该像这样
but it should work like this
当我选择ABC公司
只有两个条款清单应显示
only two provision list should display
Alberta
Ontario
不应该所有显示任何机构可以帮助我如何重写我的code
not should all display can any body help me how to rewrite my code
推荐答案
这应该做到这一点:
XMLParser parser = new XMLParser();
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList n1 = doc.getElementsByTagName("company");
// looping through all item nodes <item>
for (int i = 0; i < n1.getLength(); i++) {
Element e = (Element) n1.item(i);
System.out.println("name node " +parser.getValue(e, "name"));
NodeList children = e.getChildNodes();
for (int j = 0; j < children.getLength(); j++) {
Node child = children.item(j);
if (child.getNodeName().equalsIgnoreCase("province")) {
System.out.println("name node " + parser.getValue((Element)child, "name"));
}
}
}
这篇关于在Android中使用DOM解析XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文