Android: Converting an XML from the raw folder to

2019-05-15 07:49发布

I have come across an issue that is driving me nuts xD ... (Firstly: Hello everyone! For some reason I cant prepend the "Hello everyone" at the beginning of the post in edits oO...)

The use-case is as follows: I have an xml file stored in my project's raw folder. The xml file look something like this:

<myxml ....>Some text<innerelement ... /></myxml>

I know there is a folder called xml but what I need is a string - not an XmlResourceParser (which you get when calling context.getResources().getXml(id)).

Now, what I do to load this raw xml file is:

context.getResources().openRawResource(R.raw.myfile)

This returns me an InputStream, which I then try to convert to a String using the following code:

try {
   byte[] buffer = new byte[stream.available()];
   stream.read(buffer);
   stream.close();
   return new String(buffer);
} catch (IOException e) {
   // Error handling
}

If I now print the resulting String to logcat, all I get is a bunch of squares, spread across a few lines.

So I guess I must be missing something there ... I tried a few ways of converting this input stream to String already and all of them ended up with the same result (see image above) ...

Thanks in advance & best regards,

zainodis

UPDATE 12.11.2011

I tried saving the xml file in the raw folder using the extension .txt instead of .xml. Now the conversion fails with an OutOfMemory exception oO. I tried different way to convert the data using suggestions from this thread - all resulting in a OutOfMemoryException:

11-12 06:09:22.671: I/TestRunner(652): java.lang.OutOfMemoryError
11-12 06:09:22.671: I/TestRunner(652):  at java.util.Scanner.expandBuffer(Scanner.java:2183)
11-12 06:09:22.671: I/TestRunner(652):  at java.util.Scanner.readMore(Scanner.java:2143)
11-12 06:09:22.671: I/TestRunner(652):  at java.util.Scanner.findPostDelimiter(Scanner.java:2121)
11-12 06:09:22.671: I/TestRunner(652):  at java.util.Scanner.setTokenRegion(Scanner.java:2031)
11-12 06:09:22.671: I/TestRunner(652):  at java.util.Scanner.next(Scanner.java:1017)
11-12 06:09:22.671: I/TestRunner(652):  at java.util.Scanner.next(Scanner.java:993)
11-12 06:09:22.671: I/TestRunner(652):  at de.softcon.mobileapp.framework.core.configuration.utility.StringUtils.InputStreamToString(StringUtils.java:69)
11-12 06:09:22.671: I/TestRunner(652):  at de.softcon.mobileoffences.domainmodel.serialization.test.CorpusDelictiFactoryTest.testSerialization(CorpusDelictiFactoryTest.java:32)
11-12 06:09:22.671: I/TestRunner(652):  at java.lang.reflect.Method.invokeNative(Native Method)
11-12 06:09:22.671: I/TestRunner(652):  at java.lang.reflect.Method.invoke(Method.java:507)
11-12 06:09:22.671: I/TestRunner(652):  at junit.framework.TestCase.runTest(TestCase.java:154)
11-12 06:09:22.671: I/TestRunner(652):  at junit.framework.TestCase.runBare(TestCase.java:127)
11-12 06:09:22.671: I/TestRunner(652):  at junit.framework.TestResult$1.protect(TestResult.java:106)
11-12 06:09:22.671: I/TestRunner(652):  at junit.framework.TestResult.runProtected(TestResult.java:124)
11-12 06:09:22.671: I/TestRunner(652):  at junit.framework.TestResult.run(TestResult.java:109)
11-12 06:09:22.671: I/TestRunner(652):  at junit.framework.TestCase.run(TestCase.java:118)
11-12 06:09:22.671: I/TestRunner(652):  at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
11-12 06:09:22.671: I/TestRunner(652):  at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
11-12 06:09:22.671: I/TestRunner(652):  at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
11-12 06:09:22.671: I/TestRunner(652):  at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)

3条回答
我命由我不由天
2楼-- · 2019-05-15 07:54

Judging by your description it would seem that the XML file placed in raw folder was compiled like the rest of XML files before the packaging. I haven't tried that myself so I can't say for sure. Maybe you could try renaming your XML file to have a .txt extension and then reading it. Since you only need it as a String.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-05-15 07:55

I put this code in onCreate(...). It can show the string in the xml file.

InputStream stream = getResources().openRawResource(R.raw.data);
try {
        byte[] buffer = new byte[stream.available()];
        stream.read(buffer);
        stream.close();
        Log.i("xml", new String(buffer));
} catch (IOException e) {
    // Error handling
}
查看更多
做自己的国王
4楼-- · 2019-05-15 08:04

I'm not sure if you are still looking for a solution, but if you or anyone else is looking to solve this, here's what I did and got it to work. I used exactly what @wannik did but specified the charsetName I wanted to use. So my code looked something like this:

InputStream stream = getResources().openRawResource(R.raw.data);
try {
    byte[] buffer = new byte[stream.available()];
    stream.read(buffer);        
    stream.close();
    String xml = new String(buffer, "UTF-8");      // you just need to specify the charsetName   
} catch (IOException e) {
    // Error handling
}

Hope this helps.

查看更多
登录 后发表回答