2011-04-19 108 views
0

林能夠pasre和使用下面的方法顯示在這個page的XML ..安卓:XML解析問題

private void loadTopMoversData() { 
    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); 
    try { 
     String xml = XMLfunctions.getXML(TOPMOVERS_XML); 
     Document doc = XMLfunctions.XMLfromString(xml); 
     NodeList nodes = doc.getElementsByTagName("scrip"); 

     for (int i = 0; i < nodes.getLength(); i++) {  
      HashMap<String, String> map = new HashMap<String, String>();  

      Element e = (Element)nodes.item(i); 
      map.put("name", XMLfunctions.getValue(e, "name")); 
      map.put("currprice", XMLfunctions.getValue(e, "currprice")); 
      map.put("percentage", XMLfunctions.getValue(e, "percentage")); 
      map.put("code", XMLfunctions.getValue(e, "code")); 
      mylist.add(map); 

      Log.i("tag", XMLfunctions.getValue(e, "headline") + " " + XMLfunctions.getValue(e, "time")); 
     } 
    } 

但是我無法解析用同樣的方法在此page的XML,但適當的標籤。我究竟做錯了什麼?

private void loadNewsData() { 
    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); 
    try { 

     String xml = XMLfunctions.getXML(NEWS_XML); 
     Document doc = XMLfunctions.XMLfromString(xml); 
     NodeList nodes = doc.getElementsByTagName("data"); 
     ArrayList<String> links = new ArrayList<String>(); 
     for (int i = 0; i < nodes.getLength(); i++) { 
      Element e = (Element)nodes.item(i); 

      HashMap<String, String> map = new HashMap<String, String>();  
      map.put("headline", XMLfunctions.getValue(e, "headline")); 
      map.put("link", XMLfunctions.getValue(e, "link")); 
      map.put("time", XMLfunctions.getValue(e, "time")); 
      links.add(XMLfunctions.getValue(e, "link")); 
      mylist.add(map);    

      Log.i("tag", XMLfunctions.getValue(e, "headline") + " " + XMLfunctions.getValue(e, "time")); 
     } 

    } catch (Exception e) { 
     Log.e("tag", e.getMessage()); 
    } 
}` 

這是我XmlFunctions.java

public class XMLfunctions { 

public final static Document XMLfromString(String xml){ 

    Document doc = null; 

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
    try { 

     DocumentBuilder db = dbf.newDocumentBuilder(); 

     InputSource is = new InputSource(); 
     is.setCharacterStream(new StringReader(xml)); 
     doc = db.parse(is); 

    } catch (ParserConfigurationException e) { 
     System.out.println("XML parse error: " + e.getMessage()); 
     return null; 
    } catch (SAXException e) { 
     System.out.println("Wrong XML file structure: " + e.getMessage()); 
     return null; 
    } catch (IOException e) { 
     System.out.println("I/O exeption: " + e.getMessage()); 
     return null; 
    } 

    return doc; 

} 

public final static String getElementValue(Node elem) { 
    Node kid; 
    if(elem != null){ 
     if (elem.hasChildNodes()){ 
      for(kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling()){ 
       if(kid.getNodeType() == Node.TEXT_NODE ){ 
        return kid.getNodeValue(); 
       } 
      } 
     } 
    } 
    return ""; 
} 

public static String getXML(String webURL){  
     String line = null; 

     try { 

      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(webURL); 

      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      line = EntityUtils.toString(httpEntity); 

     } catch (UnsupportedEncodingException e) { 
      line = "<results status=\"error\"><msg>Can't connect to server</msg></results>"; 
     } catch (MalformedURLException e) { 
      line = "<results status=\"error\"><msg>Can't connect to server</msg></results>"; 
     } catch (IOException e) { 
      line = "<results status=\"error\"><msg>Can't connect to server</msg></results>"; 
     } 

     return line; 

} 

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; 
} 

public static String getValue(Element item, String str) {  
    NodeList n = item.getElementsByTagName(str);   
    return XMLfunctions.getElementValue(n.item(0)); 
} 
} 

有它的東西做的xml文件?請幫忙。由於

編輯: 可有人也請告訴我如何解析與「&」 XML在其與此解析器?

+0

你得到什麼錯誤,而分析第二個文件? – Olegas 2011-04-19 10:21:47

+0

我可以直接說出您解析XML的方式,並且我可以建議您使用像Simple這樣的基於註釋的解析器。我甚至寫了一篇關於如何在這裏使用它的博客文章:http://massaioli.homelinux.com/wordpress/2011/04/21/simple-xml-in-android-1-5-and-up/ – 2011-04-21 04:55:09

回答

0

我想通了這個問題。在XMLfunctions.java,它檢查是否節點是TEXT_NODE,顯然在XML節點不是一個..

if(kid.getNodeType() == Node.TEXT_NODE ){ 
       return kid.getNodeValue(); 
} 

所以,我刪除了條件,現在可以正常工作。由於

0

您可能需要重新檢查你的XML節點名,區分大小寫。還檢查你的鏈接數組是否正常?

0

第二進是不是正確的XML。正如你認爲&沒有正確逃脫。它需要像這樣:&amp;請參閱herehere

然而,有些解析器可以容忍這一點,就不會出錯了。如果這些解析器不是一個。

你既可以嘗試一些不同的解析器或者你可以嘗試修復與飼料發現你開始解析之前更換。如果你這樣做,你必須要小心,不要再逃避已經被轉義字符,否則你會最終的東西是這樣的:&amp;amp;