从xml文件夹Android中读取文件

时间:2013-12-17 13:38:54

标签: android android-asynctask android-xml android-resources

我需要从我的xml文件夹中读取一个xml文件,但我getting no such file错误。任何人都可以告诉我如何从android中的xml文件夹中读取XML文件?

这是我的代码:

InputSource inputSource = new InputSource(getResources().openRawResource(
                 getResId("R.xml." + fileName.substring(0,fileName.length() - 4),
                 AlbumDetails.class)));

并且logCat输出:

12-17 14:32:59.513: W/System.err(3563): java.lang.NoSuchFieldException: R.xml.quran_simple
12-17 14:32:59.513: W/System.err(3563):     at java.lang.Class.getDeclaredField(Class.java:631)
12-17 14:32:59.513: W/System.err(3563):     at com.darkovski.quran.AlbumDetails.getResId(AlbumDetails.java:250)
12-17 14:32:59.513: W/System.err(3563):     at com.darkovski.quran.AlbumDetails$3.doInBackground(AlbumDetails.java:282)
12-17 14:32:59.513: W/System.err(3563):     at com.darkovski.quran.AlbumDetails$3.doInBackground(AlbumDetails.java:1)
12-17 14:32:59.513: W/System.err(3563):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
12-17 14:32:59.513: W/System.err(3563):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-17 14:32:59.513: W/System.err(3563):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-17 14:32:59.513: W/System.err(3563):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
12-17 14:32:59.513: W/System.err(3563):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-17 14:32:59.513: W/System.err(3563):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-17 14:32:59.513: W/System.err(3563):     at java.lang.Thread.run(Thread.java:856)
12-17 14:32:59.513: W/ResourceType(3563): No known package when getting value for resource number 0xffffffff
12-17 14:32:59.513: W/dalvikvm(3563): threadid=16: thread exiting with uncaught exception (group=0x2b542210)
12-17 14:32:59.523: E/Burstly SDK v.1.34.0.46238 BackgroundUncaughtExceptionHandler(3563): 17-12-13 14:32:59:524 Unexpected problem on thread AsyncTask #3: An error occured while executing doInBackground()
12-17 14:32:59.523: E/Burstly SDK v.1.34.0.46238 BackgroundUncaughtExceptionHandler(3563): 17-12-13 14:32:59:530 java.lang.RuntimeException: An error occured while executing doInBackground()
12-17 14:32:59.523: E/Burstly SDK v.1.34.0.46238 BackgroundUncaughtExceptionHandler(3563):  at android.os.AsyncTask$3.done(AsyncTask.java:278)
12-17 14:32:59.523: E/Burstly SDK v.1.34.0.46238 BackgroundUncaughtExceptionHandler(3563):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-17 14:32:59.523: E/Burstly SDK v.1.34.0.46238 BackgroundUncaughtExceptionHandler(3563):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-17 14:32:59.523: E/Burstly SDK v.1.34.0.46238 BackgroundUncaughtExceptionHandler(3563):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-17 14:32:59.523: E/Burstly SDK v.1.34.0.46238 BackgroundUncaughtExceptionHandler(3563):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-17 14:32:59.523: E/Burstly SDK v.1.34.0.46238 BackgroundUncaughtExceptionHandler(3563):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
12-17 14:32:59.523: E/Burstly SDK v.1.34.0.46238 BackgroundUncaughtExceptionHandler(3563):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-17 14:32:59.523: E/Burstly SDK v.1.34.0.46238 BackgroundUncaughtExceptionHandler(3563):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-17 14:32:59.523: E/Burstly SDK v.1.34.0.46238 BackgroundUncaughtExceptionHandler(3563):  at java.lang.Thread.run(Thread.java:856)
12-17 14:32:59.523: E/Burstly SDK v.1.34.0.46238 BackgroundUncaughtExceptionHandler(3563): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0xffffffff
12-17 14:32:59.523: E/Burstly SDK v.1.34.0.46238 BackgroundUncaughtExceptionHandler(3563):  at android.content.res.Resources.getValue(Resources.java:1018)
12-17 14:32:59.523: E/Burstly SDK v.1.34.0.46238 BackgroundUncaughtExceptionHandler(3563):  at android.content.res.Resources.openRawResource(Resources.java:943)
12-17 14:32:59.523: E/Burstly SDK v.1.34.0.46238 BackgroundUncaughtExceptionHandler(3563):  at android.content.res.Resources.openRawResource(Resources.java:925)
12-17 14:32:59.523: E/Burstly SDK v.1.34.0.46238 BackgroundUncaughtExceptionHandler(3563):  at com.darkovski.quran.AlbumDetails$3.doInBackground(AlbumDetails.java:281)
12-17 14:32:59.523: E/Burstly SDK v.1.34.0.46238 BackgroundUncaughtExceptionHandler(3563):  at com.darkovski.quran.AlbumDetails$3.doInBackground(AlbumDetails.java:1)
12-17 14:32:59.523: E/Burstly SDK v.1.34.0.46238 BackgroundUncaughtExceptionHandler(3563):  at android.os.AsyncTask$2.call(AsyncTask.java:264)
12-17 14:32:59.523: E/Burstly SDK v.1.34.0.46238 BackgroundUncaughtExceptionHandler(3563):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-17 14:32:59.523: E/Burstly SDK v.1.34.0.46238 BackgroundUncaughtExceptionHandler(3563):  ... 5 more

4 个答案:

答案 0 :(得分:0)

在res /文件夹中,所有xml文件都是预编译的,而在assets /文件夹中则不是。

所以使用它你想从res文件夹中解析它

XmlResourceParser xrp = ctx.getAssets().openXmlResourceParser("res/xml/castle1.tmx");

否则 使用可以使用

InputStream object = this.getResources()
                 .openRawResource(R.raw.fileName);

答案 1 :(得分:0)

如果您需要输入流,只需将xml放入原始资源并使用

打开它

InputStream inputStream = getResources().openRawResource(R.raw.fileName);

答案 2 :(得分:0)

如果您确实需要源文本,则应该以这种方式工作:

(使用XmlResourceParser)

@Throws(IOException::class, XmlPullParserException::class)
fun readXML(context: Context, idRes: Int): String? {
    return if (idRes != View.NO_ID)
        context.resources.getXml(idRes).getAllXML()
    else
        null
}

@Throws(IOException::class, XmlPullParserException::class)
fun XmlResourceParser.getAllXML(): String {
    var str = ""
    var eventType = eventType
    while (eventType != XmlPullParser.END_DOCUMENT) {
        if (eventType == XmlPullParser.START_TAG) {
            str += "<${name}"
            for (i in 0..attributeCount) {
                val attrName = getAttributeName(i)
                val attrValue = getAttributeValue(i)
                str += "${attrName}=\"${attrValue}\""
            }
            str += ">"
            str += nextText()
        }
        if (eventType == XmlPullParser.CDSECT) {
            str += "<![CDATA[${nextText()}]]>"
        }
        if (eventType == XmlPullParser.END_TAG) {
            str += "</${name}>"
        }
        eventType = next()
    }
    return str
}

答案 3 :(得分:-1)

InputStream is = getResources().getAssets().open("abc.txt");
String result= convertStreamToString(is);

public static String convertStreamToString(InputStream is)
        throws IOException {
        Writer writer = new StringWriter();
    char[] buffer = new char[2048];
    try {
        Reader reader = new BufferedReader(new InputStreamReader(is,
                "UTF-8"));
        int n;
        while ((n = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, n);
        }
    } finally {
        is.close();
    }
    String text = writer.toString();
    return text;
}

在此代码中..而不是“资产”使用您的文件夹。