【发布时间】:2012-11-22 03:46:41
【问题描述】:
所以在您的指南之后,我将代码更改为这个,但应用程序只是挂起...... 下一步是什么?从
调用 getXMLDocument xml = XMLemailF.getXML();
.
public static Document getXML(){
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// get a document builder
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//parse using builder to get DOM representation of the XML file
Document doc = null;
try {
doc = db.parse("/xxxxxx/res/xml/email.xml");
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return doc;
}
这叫
public static int numResults(Document doc){
Node results = doc.getDocumentElement();
int res = -1;
try{
res = Integer.valueOf(results.getAttributes().getNamedItem("count").getNodeValue());
}catch(Exception e ){
res = -1;
}
return res;
}
通过这个
...
Document xml = XMLemailF.getXML();
int numResults = XMLemailF.numResults(xml);
if((numResults <= 0)){
Toast.makeText(contactus_email.this, "No results found", Toast.LENGTH_LONG).show();
finish();
}
NodeList nodes = xml.getElementsByTagName("result");
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
map.put("email", XMLemailF.getValue(e, "email"));
map.put("line1", XMLemailF.getValue(e, "line1"));
mylist.add(map);
...
有什么想法吗?
【问题讨论】:
-
你想做什么?顺便说一句,你的问题的标题已经和内容无关了……这肯定和原来的问题不一样
-
我不知道如何在 cmets 中粘贴代码。我只是希望它们被解析成一个列表视图。如果我从互联网上获取 xml,一切正常。原始代码提供字符串输出,但您的部分是文档输出。我在那里堆积...如果出于存档原因无法帮助我,将重新发布第一个问题。提前谢谢
标签: java android xml parsing url