2011-04-25 71 views
0

我解析从一个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");不起作用。

+1

是否抛出异常?什么是logcat输出? – 2011-04-25 13:23:35

+1

告诉我你在Logcat中发生了什么错误以及你想解析的xml中的哪个标记 – 2011-04-25 13:24:38

+0

它显示异常是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

回答

0

请参阅我的博客,我已经给出了详细解释,http://sankarganesh-info-exchange.blogspot.com/2011/04/parsing-data-from-internet-and-creating.html,并确保您已在您的Manifest文件中添加Internet权限。

如果您已通过Myblog走了,那么你就能够发现你做下面一行是错误

Document document = parser.parse("http://demo.greatinnovus.com/restingspot/search?userid=xxxxxxxxxxxxxxxx&firstname=a&lastname=a"); 

使用这样

URL url =new URL("http://demo.greatinnovus.com/restingspot/search?userid=xxxxxxxxxxxxxxxx&firstname=a&lastname=a"); 
Document document= parser.parse(new InputSource(url.openStream())); 
+0

hi sangar ganesh i在Logcat中获得以下内容: 它显示异常是InputSource需要流或读取器logcat 04-25 19:01:22.443:ERROR/rsport(2177):{chap = hjdasgdas } 04-25 19:01:22.453:ERROR/listview(2177):[email protected] 04-25 19:01:22.563:ERROR/rsport-(2177):function1 04-25 19:01:22.563 :错误/ rsport-(2177):function2 04-25 19:01:22.583:错误/ rsport ---(2177):InputSource需要流或读取器 – 2011-04-25 13:35:43

+0

感谢shangar它工作 – 2011-04-25 13:51:41