我无法读取文件使用saxParser

时间:2011-04-23 04:28:42

标签: android

我是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

1 个答案:

答案 0 :(得分:3)

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

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

Ammm我认为你应该使用InputStream而不是String构造函数。