gpt4 book ai didi

android - 我无法使用 saxParser 读取文件

转载 作者:行者123 更新时间:2023-11-29 18:20:36 27 4
gpt4 key购买 nike

我是 Android 新手,我对 SAXParser 有疑问我有一个 XML 文件,我想将此文件传递给 ArrayList,但我无法读取该文件...

这是我的代码

    public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.index);

FileParserSax saxParcer = new FileParserSax(getFilesDir().getAbsoluteFile() + "/datos.xml");
ArrayList<Meansurement> procesos = saxParcer.parse();
System.out.println("aqui");
}

我的函数解析器是这样的..

public ArrayList<Meansurement> parse(){

SAXParserFactory factory= SAXParserFactory.newInstance();
try{
SAXParser parser= factory.newSAXParser();
SaxMeansurementHandler handler = new SaxMeansurementHandler();
parser.parse(new File(ruta), handler);
return handler.getMeansurements();
}catch (Exception e) {
throw new RuntimeException(e);
}
}

但是当我到达这一行时 parser.parse(new File(ruta), handler);失败!!!

这是错误

04-23 00:18:13.308: ERROR/AndroidRuntime(2533): Uncaught handler: thread main exiting due to uncaught exception
04-23 00:18:13.559: ERROR/AndroidRuntime(2533): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cronox/com.cronox.Index}: java.lang.RuntimeException: java.io.IOException: Couldn't open /data/data/com.cronox/files/datos.xml
04-23 00:18:13.559: ERROR/AndroidRuntime(2533): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
04-23 00:18:13.559: ERROR/AndroidRuntime(2533): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
04-23 00:18:13.559: ERROR/AndroidRuntime(2533): at android.app.ActivityThread.access$2100(ActivityThread.java:116)
04-23 00:18:13.559: ERROR/AndroidRuntime(2533): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
04-23 00:18:13.559: ERROR/AndroidRuntime(2533): at android.os.Handler.dispatchMessage(Handler.java:99)
04-23 00:18:13.559: ERROR/AndroidRuntime(2533): at android.os.Looper.loop(Looper.java:123)
04-23 00:18:13.559: ERROR/AndroidRuntime(2533): at android.app.ActivityThread.main(ActivityThread.java:4203)
04-23 00:18:13.559: ERROR/AndroidRuntime(2533): at java.lang.reflect.Method.invokeNative(Native Method)
04-23 00:18:13.559: ERROR/AndroidRuntime(2533): at java.lang.reflect.Method.invoke(Method.java:521)
04-23 00:18:13.559: ERROR/AndroidRuntime(2533): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
04-23 00:18:13.559: ERROR/AndroidRuntime(2533): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
04-23 00:18:13.559: ERROR/AndroidRuntime(2533): at dalvik.system.NativeStart.main(Native Method)
04-23 00:18:13.559: ERROR/AndroidRuntime(2533): Caused by: java.lang.RuntimeException: java.io.IOException: Couldn't open /data/data/com.cronox/files/datos.xml
04-23 00:18:13.559: ERROR/AndroidRuntime(2533): at com.cronox.FileParserSax.parse(FileParserSax.java:25)
04-23 00:18:13.559: ERROR/AndroidRuntime(2533): at com.cronox.Index.onCreate(Index.java:35)
04-23 00:18:13.559: ERROR/AndroidRuntime(2533): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
04-23 00:18:13.559: ERROR/AndroidRuntime(2533): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
04-23 00:18:13.559: ERROR/AndroidRuntime(2533): ... 11 more
04-23 00:18:13.559: ERROR/AndroidRuntime(2533): Caused by: java.io.IOException: Couldn't open /data/data/com.cronox/files/datos.xml
04-23 00:18:13.559: ERROR/AndroidRuntime(2533): at org.apache.harmony.xml.ExpatParser.openUrl(ExpatParser.java:744)
04-23 00:18:13.559: ERROR/AndroidRuntime(2533): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:300)
04-23 00:18:13.559: ERROR/AndroidRuntime(2533): at javax.xml.parsers.SAXParser.parse(SAXParser.java:361)
04-23 00:18:13.559: ERROR/AndroidRuntime(2533): at javax.xml.parsers.SAXParser.parse(SAXParser.java:303)
04-23 00:18:13.559: ERROR/AndroidRuntime(2533): at com.cronox.FileParserSax.parse(FileParserSax.java:22)
04-23 00:18:13.559: ERROR/AndroidRuntime(2533): ... 14 more
04-23 00:18:13.559: ERROR/AndroidRuntime(2533): Caused by: java.net.MalformedURLException: Protocol not found: /data/data/com.cronox/files/datos.xml
04-23 00:18:13.559: ERROR/AndroidRuntime(2533): at java.net.URL.<init>(URL.java:285)
04-23 00:18:13.559: ERROR/AndroidRuntime(2533): at java.net.URL.<init>(URL.java:164)
04-23 00:18:13.559: ERROR/AndroidRuntime(2533): at org.apache.harmony.xml.ExpatParser.openUrl(ExpatParser.java:737)
04-23 00:18:13.559: ERROR/AndroidRuntime(2533): ... 18 more

最佳答案

使用 file:///data/data/com.cronox/files/datos.xml 。喜欢:

FileParserSax saxParcer = new FileParserSax("file://" + getFilesDir().getAbsoluteFile() + "/datos.xml");

嗯,我认为您应该使用 InputStream 而不是 String 构造函数。

关于android - 我无法使用 saxParser 读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5762175/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com