Java 获取列表<;字符串>;在android中创建XML文件
我是android新手,想知道如何在android中获得XML文件数组 我尝试了很多方法,但都给了我一个NullPointerException 我的xml文件夹目录是res/xml/db.xmlJava 获取列表<;字符串>;在android中创建XML文件,java,android,xml,Java,Android,Xml,我是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 {
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;
}
}
提前感谢,请让我知道这是否是任何重复,因为我已经阅读了几个问题之前,他们都不能帮助我足够!谢谢 只需在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的形式访问它我找到了解决方案,使用了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;
}
publicstaticarraylistmodel_parse(inputstreamurl)抛出IOException,XmlPullParserException{
最终ArrayList结果=新ArrayList(1000);
XmlPullParserFactory工厂=XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp=factory.newPullParser();
设置输入(url,“UTF-8”);
xpp.nextTag();
require(XmlPullParser.START_标记,null,“NewDataSet”);
while(xpp.nextTag()==XmlPullParser.START_标记){
require(XmlPullParser.START_标记,null,“Hispania_DB”);
HModel mdl=新的HModel();
xpp.nextTag();
require(XmlPullParser.START_标记,null,“Model”);
mdl.model=xpp.nextText();
require(XmlPullParser.END_标记,null,“Model”);
xpp.nextTag();
require(XmlPullParser.START_标记,null,“Capacity”);
mdl.capacity=Double.parseDouble(xpp.nextText());
expp.require(XmlPullParser.END_标记,null,“Capacity”);
xpp.nextTag();
require(XmlPullParser.START_标记,null,“Surface”);
mdl.surface=Double.parseDouble(xpp.nextText());
require(XmlPullParser.END_标记,null,“Surface”);
xpp.nextTag();
expp.require(XmlPullParser.START_标记,null,“Air_x0020_Flow”);
mdl.afflow=xpp.nextText();
expp.require(XmlPullParser.END_标记,null,“Air_x0020_Flow”);
xpp.nextTag();
require(XmlPullParser.START_标记,null,“Air_x0020_Throw”);
mdl.airthow=xpp.nextText();
require(XmlPullParser.END_标记,null,“Air_x0020_Throw”);
xpp.nextTag();
require(XmlPullParser.START_标记,null,“Number_Fans”);
mdl.nm_fans=xpp.nextText();
require(XmlPullParser.END_标记,null,“Number_Fans”);
xpp.nextTag();
require(XmlPullParser.START_标记,null,“Fan_直径”);
mdl.fandiam=xpp.nextText();
expp.require(XmlPullParser.END_标记,null,“Fan_直径”);
xpp.nextTag();
require(XmlPullParser.START_标记,null,“L”);
mdl.l=xpp.nextText();
require(XmlPullParser.END_标记,null,“L”);
xpp.nextTag();
require(XmlPullParser.START_标记,null,“H”);
mdl.h=xpp.nextText();
require(XmlPullParser.END_标记,null,“H”);
xpp.nextTag();
require(XmlPullParser.START_标记,null,“W”);
mdl.w=xpp.nextText();
require(XmlPullParser.END_标记,null,“W”);
xpp.nextTag();
require(XmlPullParser.START_标记,null,“Weight”);
mdl.weight=xpp.nextText();
require(XmlPullParser.END_标记,null,“Weight”);
xpp.nextTag();
require(XmlPullParser.START_标记,null,“入口”);
mdl.import=xpp.nextText();
require(XmlPullParser.END_标记,null,“入口”);
xpp.nextTag();
require(XmlPullParser.START_标记,null,“Outlet”);
mdl.outlet=xpp.nextText();
require(XmlPullParser.END_标记,null,“Outlet”);
xpp.nextTag();
expp.require(XmlPullParser.START_标记,null,“Fan_x0020_电压”);
mdl.fan_volt=xpp.nextText();
expp.require(XmlPullParser.END_标记,null,“Fan_x0020_电压”);
xpp.nextTag();
expp.require(XmlPullParser.START_标记,null,“Fan_x0020_Current”);
mdl.fan_curr=xpp.nextText();
expp.require(XmlPullParser.END_标记,null,“Fan_x0020_Current”);
xpp.nextTag();
expp.require(XmlPullParser.START_标记,null,“Total_x0020_Defrost”);
mdl.defr=xpp.nextText();
expp.require(XmlPullParser.END_标记,null,“Total_x0020_Defrost”);
xpp.nextTag();
require(XmlPullParser.END_标记,null,“Hispania_DB”);
结果:添加(mdl);
}
require(XmlPullParser.END_标记,null,“NewDataSet”);
返回结果;
}
这非常有效,谢谢大家我得到的是F流发现的FrimeNoTebug异常(我以前把它作为文件)我尝试了另一种方法,它给了我org .xMx.SAX。SAXParseExceptionLet考虑我使用这个方法,我如何检索数组?(有几个子数组)我从未尝试过。也许你可以把它们放在窝里?我可以尝试一下,然后很快发布更新……不过我强烈喜欢使用xml,而不是字符串数组。