2014-03-27 25 views
0

獲取XML文件的列表 我對android非常陌生,想知道如何在android中獲取XML文件的數組? Android的

我試了很多方法,但他們都給了我一個NullPointerException。

我的XML文件夾的目錄是RES/XML/db.xml

Document d = parseXML(this.getAssets().open("xml/db.xml")); 

還是返回了 '找不到文件' 異常。

而這裏的parseXML方法:

public Document parseXML(InputStream f) { 
    try { 
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
     dbf.setNamespaceAware(false); 
     dbf.setValidating(false); 
     DocumentBuilder db = dbf.newDocumentBuilder(); 
     return db.parse(f); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     return null; 
    } 
} 

在此先感謝,請讓我知道,如果這是任何重複的,因爲我讀過幾個問題之前,他們都無法幫我夠!謝謝!

+1

任何logcat輸出? – donfuxx

+0

所有我得到的FileNotFound異常時,給予f流(我有它作爲文件之前) – John

+0

我試着另一種方式,它給了我org.xml.sax.SAXParseException – John

回答

0

我找到了解決辦法,我用XMLPullParser並保存在XML文件到原始目錄。

public static ArrayList<HModel> model_parse(InputStream url) throws IOException, XmlPullParserException { 
    final ArrayList<HModel> results = new ArrayList<HModel>(1000); 

    XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
    factory.setNamespaceAware(true); 
    XmlPullParser xpp = factory.newPullParser(); 

    xpp.setInput(url, "UTF-8"); 

    xpp.nextTag(); 
    xpp.require(XmlPullParser.START_TAG, null, "NewDataSet"); 
    while (xpp.nextTag() == XmlPullParser.START_TAG) { 
     xpp.require(XmlPullParser.START_TAG, null, "Hispania_DB"); 
     HModel mdl = new HModel(); 
     xpp.nextTag(); 
     xpp.require(XmlPullParser.START_TAG, null, "Model"); 
     mdl.model = xpp.nextText(); 
     xpp.require(XmlPullParser.END_TAG, null, "Model"); 

     xpp.nextTag(); 
     xpp.require(XmlPullParser.START_TAG, null, "Capacity"); 
     mdl.capacity = Double.parseDouble(xpp.nextText()); 
     xpp.require(XmlPullParser.END_TAG, null, "Capacity"); 

     xpp.nextTag(); 
     xpp.require(XmlPullParser.START_TAG, null, "Surface"); 
     mdl.surface = Double.parseDouble(xpp.nextText()); 
     xpp.require(XmlPullParser.END_TAG, null, "Surface"); 

     xpp.nextTag(); 
     xpp.require(XmlPullParser.START_TAG, null, "Air_x0020_Flow"); 
     mdl.airflow = xpp.nextText(); 
     xpp.require(XmlPullParser.END_TAG, null, "Air_x0020_Flow"); 

     xpp.nextTag(); 
     xpp.require(XmlPullParser.START_TAG, null, "Air_x0020_Throw"); 
     mdl.airthrow = xpp.nextText(); 
     xpp.require(XmlPullParser.END_TAG, null, "Air_x0020_Throw"); 

     xpp.nextTag(); 
     xpp.require(XmlPullParser.START_TAG, null, "Number_Fans"); 
     mdl.nm_fans = xpp.nextText(); 
     xpp.require(XmlPullParser.END_TAG, null, "Number_Fans"); 

     xpp.nextTag(); 
     xpp.require(XmlPullParser.START_TAG, null, "Fan_Diameter"); 
     mdl.fandiam = xpp.nextText(); 
     xpp.require(XmlPullParser.END_TAG, null, "Fan_Diameter"); 

     xpp.nextTag(); 
     xpp.require(XmlPullParser.START_TAG, null, "L"); 
     mdl.l = xpp.nextText(); 
     xpp.require(XmlPullParser.END_TAG, null, "L"); 

     xpp.nextTag(); 
     xpp.require(XmlPullParser.START_TAG, null, "H"); 
     mdl.h = xpp.nextText(); 
     xpp.require(XmlPullParser.END_TAG, null, "H"); 

     xpp.nextTag(); 
     xpp.require(XmlPullParser.START_TAG, null, "W"); 
     mdl.w = xpp.nextText(); 
     xpp.require(XmlPullParser.END_TAG, null, "W"); 

     xpp.nextTag(); 
     xpp.require(XmlPullParser.START_TAG, null, "Weight"); 
     mdl.weight = xpp.nextText(); 
     xpp.require(XmlPullParser.END_TAG, null, "Weight"); 

     xpp.nextTag(); 
     xpp.require(XmlPullParser.START_TAG, null, "Inlet"); 
     mdl.inlet = xpp.nextText(); 
     xpp.require(XmlPullParser.END_TAG, null, "Inlet"); 

     xpp.nextTag(); 
     xpp.require(XmlPullParser.START_TAG, null, "Outlet"); 
     mdl.outlet = xpp.nextText(); 
     xpp.require(XmlPullParser.END_TAG, null, "Outlet"); 

     xpp.nextTag(); 
     xpp.require(XmlPullParser.START_TAG, null, "Fan_x0020_Voltage"); 
     mdl.fan_volt = xpp.nextText(); 
     xpp.require(XmlPullParser.END_TAG, null, "Fan_x0020_Voltage"); 

     xpp.nextTag(); 
     xpp.require(XmlPullParser.START_TAG, null, "Fan_x0020_Current"); 
     mdl.fan_curr = xpp.nextText(); 
     xpp.require(XmlPullParser.END_TAG, null, "Fan_x0020_Current"); 

     xpp.nextTag(); 
     xpp.require(XmlPullParser.START_TAG, null, "Total_x0020_Defrost"); 
     mdl.defr = xpp.nextText(); 
     xpp.require(XmlPullParser.END_TAG, null, "Total_x0020_Defrost"); 

     xpp.nextTag(); 
     xpp.require(XmlPullParser.END_TAG, null, "Hispania_DB"); 

     results.add(mdl); 
    } 
    xpp.require(XmlPullParser.END_TAG, null, "NewDataSet"); 

    return results; 
} 

這工作完美,謝謝你們!

0

只需在res/values目錄下創建文件arrays.xml即可。然後你就可以在你的元素,比如添加:

<resources> 
<string-array name="items"> 
    <item>lorem</item> 
    <item >ipsum</item> 
    <item >dolor</item> 
    <item >sit</item> 
    <item >amet</item> 
</string-array> 
</resources> 

然後,您可以訪問此爲R.array.items

+0

讓我們考慮我使用這種方法,我如何檢索數組? (有幾個子陣列) – John

+0

我從來沒有嘗試過。也許你可以嵌套它們?我可以嘗試一下,很快發佈更新... – ucsunil

+0

我強烈喜歡使用xml,但不是字符串數組。 – John