Android:将XML从原始文件夹转换为字符串 [英] Android: Converting an XML from the raw folder to string

查看:133
本文介绍了Android:将XML从原始文件夹转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个让我疯狂的问题xD ......(首先:大家好!由于某种原因,我无法在编辑开头的帖子前面加上大家好...)

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...)

用例如下:我有一个xml文件存储在我项目的原始文件夹中。 xml文件如下所示:

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>

我知道有一个名为xml的文件夹,但我需要的是一个字符串 - 而不是XmlResourceParser(你在调用context.getResources()。getXml(id))时得到。

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)).

现在,我要加载这个原始xml文件的是:

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

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

这会返回一个InputStream,然后我尝试使用以下代码转换为String:

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
}

如果我现在将生成的字符串打印到logcat,我得到的就是一堆正方形,分布在几行上。

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

所以我想我一定不能错过那里的东西......我尝试了几种将这个输入流转换为String的方法,所有这些方法都得到了相同的结果(见上图)......

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

我尝试使用扩展名.txt而不是.xml将xml文件保存在raw文件夹中。现在转换失败,出现OutOfMemory异常oO。我尝试了使用此主题 - 全部导致OutOfMemoryException:

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)


推荐答案

我把这个放了 onCreate(...)中的代码。它可以在xml文件中显示字符串。

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
}

这篇关于Android:将XML从原始文件夹转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆