I need to read an xml file from my xml folder, and but I'm getting no such file
error. Can anyone tell me how can i read XML file from the xml folder in android?
Here is my code:
InputSource inputSource = new InputSource(getResources().openRawResource(
getResId("R.xml." + fileName.substring(0,fileName.length() - 4),
AlbumDetails.class)));
And logCat output:
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
In res/ folder all xml files are precompiled, whereas in assets/ folder they are not.
so use this you want to parse it from res folder
XmlResourceParser xrp = ctx.getAssets().openXmlResourceParser("res/xml/castle1.tmx");
else use can use
InputStream object = this.getResources()
.openRawResource(R.raw.fileName);
if you need an inputstream out of it simply put the xml into raw resources and open it with
InputStream inputStream = getResources().openRawResource(R.raw.fileName);
It should work this way if you realy need source text:
(Using 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
}
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;
}
In this code.. Instead of "assests" use your folder.
User contributions licensed under CC BY-SA 3.0