我解析從一個URL的XML。該網址有移動IMEI號和基於我的應用程序的搜索字符串。我把我的XML解析代碼在Android項目中不起作用。但如果我作爲單獨的Java程序運行它正在工作。請幫幫我。解析不工作在android
Log.e("rsport-", "function1");
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setIgnoringComments(true);
factory.setCoalescing(true); // Convert CDATA to Text nodes
factory.setNamespaceAware(false); // No namespaces: this is default
factory.setValidating(false); // Don't validate DTD: also default
DocumentBuilder parser = factory.newDocumentBuilder();
Log.e("rsport-", "function2");
Document document = parser.parse("http://demo.greatinnovus.com/restingspot/search?userid=xxxxxxxxxxxxxxxx&firstname=a&lastname=a");
Log.e("rsport-","function3");
NodeList sections = document.getElementsByTagName("Searchdata");
int numSections = sections.getLength();
for (int i = 0; i < numSections; i++)
{
Element section = (Element) sections.item(i);
if(section.hasChildNodes()==true){
NodeList section1=section.getChildNodes();
for(int j=0;j<section1.getLength();j++){
if(section1.item(j).hasChildNodes()==true) {
for(int k=0;k<section1.item(j).getChildNodes().getLength();k++)
xmlvalue=String.valueOf(section1.item(j).getChildNodes().item(k).getNodeValue()).trim();
arl.add(xmlvalue);
}
}
}
}
}
}
catch(Exception e){}
System.out.println("id"+id+" searchdatacount"+searchdatacount);
System.out.println("---------");
ListIterator<String> litr = arl.listIterator();
while (litr.hasNext()) {
String element = litr.next();
Log.e("rsport-", "elememt");
}
之後的Log.e("rsport-", "function2")
;不起作用。
是否拋出異常?什麼是logcat輸出? – 2011-04-25 13:23:35
告訴我你在Logcat中發生了什麼錯誤以及你想解析的xml中的哪個標記 – 2011-04-25 13:24:38
它顯示異常是InputSource需要流或者讀取器 logcat 04-25 19:01:22.443:ERROR/rsport(2177) :{chap = hjdasgdas} 04-25 19:01:22.453:錯誤/ listview(2177):[email protected] 04-25 19:01:22.563:ERROR/rsport-(2177):function1 04-25 19:01:22.563:ERROR/rsport-(2177):function2 04-25 19:01:22.583:ERROR/rsport ---(2177):InputSource需要流或讀取器 – 2011-04-25 13:32:50