解析 XML,调用 doc.getDocumentElement().getNodeName() 并得到“h1”的错误输出?
这是我的代码:(来源:http://www.androidhive。 info/2011/11/android-xml-parsing-tutorial/)
这是我的 XML 文件:已删除
运行时,出现 NoResults,并且我得到字符串“h1”,它不是我的 XML 标记 文件。 我不确定我的 xml 文件是否转换不正确,或者我无法成功找到“members”标签。
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
XMLParser parse = new XMLParser();
String xml = parse.getXmlFromUrl(url); // getting XML
Document doc = parse.getDomElement(xml); // getting DOM element
NodeList nodes = doc.getElementsByTagName(KEY_MEMBER);
// looping through all item nodes <member>
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
map.put(KEY_FNAME, parse.getValue(e, KEY_FNAME));
map.put(KEY_MNAME, parse.getValue(e, KEY_MNAME));
map.put(KEY_LNAME, parse.getValue(e, KEY_LNAME));
map.put(KEY_STATE, parse.getValue(e, KEY_STATE));
map.put(KEY_PARTY, parse.getValue(e, KEY_PARTY));
mylist.add(map);
}
if((nodes.getLength() <= 0)){
Toast.makeText(AndroidXMLParsingActivity.this, "NoResults " + doc.getDocumentElement().getNodeName(), Toast.LENGTH_LONG).show();
finish();
}
Here is my code: (source: http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/)
Here is my XML file: removed
On run, the NoResults appears and I get the string "h1" which isn't a xml tag in my file.
I am not sure if my xml file is being converted incorrectly or I can't successfully find the "members" tag.
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
XMLParser parse = new XMLParser();
String xml = parse.getXmlFromUrl(url); // getting XML
Document doc = parse.getDomElement(xml); // getting DOM element
NodeList nodes = doc.getElementsByTagName(KEY_MEMBER);
// looping through all item nodes <member>
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
map.put(KEY_FNAME, parse.getValue(e, KEY_FNAME));
map.put(KEY_MNAME, parse.getValue(e, KEY_MNAME));
map.put(KEY_LNAME, parse.getValue(e, KEY_LNAME));
map.put(KEY_STATE, parse.getValue(e, KEY_STATE));
map.put(KEY_PARTY, parse.getValue(e, KEY_PARTY));
mylist.add(map);
}
if((nodes.getLength() <= 0)){
Toast.makeText(AndroidXMLParsingActivity.this, "NoResults " + doc.getDocumentElement().getNodeName(), Toast.LENGTH_LONG).show();
finish();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于“h1”是一个 html 标签,我猜测您点击的 url 返回的是 html,而不是 xml。当然,确定这一点的一个简单方法是在解析 xml 字符串之前将其打印出来。
仅供参考,不确定在 android 上编码的限制,但在解析 xml 文档之前将其转换为字符串通常是一个坏主意(出于各种原因,从“破坏”xml 到不必要地增加内存使用量)。
Since "h1" is an html tag, i would guess that the url your are hitting is returning html, not xml. an easy way to determine this, of course, would be to print out the xml string before you parse it.
fyi, not sure about the limitations of coding on android, but it's generally a bad idea to convert an xml document to string before you parse it (for a variety of reasons ranging from "breaking" the xml to needlessly increasing the memory usage).