2012-07-25 72 views
0

我已經使用鏈接的代碼來解析xml,它的工作版本低於android 2.3,但我試圖讓它完成4.0.3它不會返回任何值。如何解決此問題。我沒有得到在日誌中我沒有任何錯誤,但得到的分析值output.Here是link I used as code如何解析Android 4.0支持的xml?

@SuppressWarnings("unused") 
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; 

} 

回答

1

使用Log.e(),而不是System.out.println()。您可能會遇到異常,但未顯示。

try { 
    ... 
} catch (ParserConfigurationException e) { 
    Log.e(TAG, "XML parse error", e); 
    return null; 
} catch (SAXException e) { 
    Log.e(TAG, "Wrong XML file structure", e); 
    return null; 
} catch (IOException e) { 
    Log.e(TAG, "I/O exeption", e); 
    return null; 
} 

(其中TAG是一個字符串常量)

+0

都能跟得上它的一樣,它沒有做扣部分東西。 – Karthik 2012-07-25 07:29:43

+0

然後我不知道。 ['DocumentBuilder.parse()'](http://www.docjar.com/html/api/org/apache/xerces/jaxp/DocumentBuilderImpl.java.html)不應該返回'null'。 (假設Xerces是使用的實現,請檢查使用'db.getClass()。getName()'。)的實現。 – 2012-07-25 07:48:35