我对android很新,我想知道如何在android中获取XML文件的数组?
我尝试了很多方法但是所有这些方法都给了我一个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;
}
}
在此先感谢,如果这有任何重复,请告诉我,因为我之前已经阅读了几个问题,而且所有问题都无法帮助我!谢谢!
答案 0 :(得分: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
进行访问答案 1 :(得分: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;
}
这非常有效,谢谢你们!