3

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)
Community
  • 1
  • 1
AgentKnopf
  • 4,295
  • 7
  • 45
  • 81

3 Answers3

5

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
}
wannik
  • 12,212
  • 11
  • 46
  • 58
  • Thanks for the hint - thing is i am already doing this ;) this is how i figured out that "If I now print the resulting String to logcat, all I get is a bunch of squares, spread across a few lines." - I already wrote this in my original post. So the problem is --> why is it not the expected xml but a bunch of squares when logging the converted String. – AgentKnopf Nov 11 '11 at 14:51
  • Is it about character encoding? – wannik Nov 11 '11 at 14:54
  • Mhhh dont think so... I made sure that the xml is formatted as UTF-8 - so I don't think it's the encoding... – AgentKnopf Nov 11 '11 at 15:18
  • I have found no solution, so I gave up on the issue - I'll mark your answer as the correct one, because that's they way it SHOULD work, but unfortunately doesn't for me, for whatever reason :( – AgentKnopf Dec 17 '11 at 11:17
1

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.

Ahmad
  • 438
  • 1
  • 6
  • 10
  • Thanks for sharing this answer :) ! I ended up putting my files into the xml folder and it works fine, even with bigger files. But it might help someone in a similar situation as I was then. – AgentKnopf Feb 06 '13 at 06:56
0

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.

Andrius
  • 423
  • 4
  • 11
  • I have tried it out, saving my xml as a .txt what I now get is out of memory exceptions, no matter which conversion method I try oO ... – AgentKnopf Nov 12 '11 at 06:10
  • I would like to add that this xml file really is NOT big at all, which makes me wonder about the OOM exception ... – AgentKnopf Nov 12 '11 at 06:15