在处理Android上的XML文件后,奇怪的字符
我正在从一个XML文件加载数据的Andorid的应用程序了。问题是,XML文件是不正确处理,我不能序列化,因为这一点。我认为这个问题是涉及到文件的编码。
I am making an andorid applicaiton which loads data from a xml file. The problem is that the xml file is not processed correctly and I am not able to serialize it, because of that. I think the problem is related to the encoding of the file.
如何我做了xml文件?从Eclipse的我已经点击新建 - >文件 - >并创建了一个空白的XML,然后我和所需信息填写它
How I made the xml file? From Eclipse I have clicked on New-> File -> and created a blank xml, then I've filled it with the needed information.
下面是XML的外观在编辑器:
Here is how the xml looks in the editor:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<categories>
<category value="Inbox"/>
<category value="Private"/>
<category value="Work"/>
<category value="Business"/>
</categories>
<todos>
<todo>
<id>1</id>
<text>Explore the app!</text>
</todo>
<todo>
<id>2</id>
<text>Add more todos!</text>
<date>2013-05-09 12:21:55 CET</date>
</todo>
</todos>
</data>
我收到来自RES / XML的XML文件,然后我加载输入流中的文件。之后,我使用String构造的文件转换为字符串。
I am getting the xml file from res/xml and then I load the file in an Input Stream. After that I am converting the file to a String using the String constructor.
下面是java code:
Here is the java code:
InputStream is = getResources().openRawResource(R.xml.startingdata);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int next = is.read();
while (next > -1) {
bos.write(next);
next = is.read();
}
bos.flush();
byte[] result = bos.toByteArray();
xmldata = new String(result,"UTF-8");
这一步后,我展现新XMLDATA来敬酒,以检查它的外观。不过,我接收到的数据,再加上一些奇怪的字符的一部分。我做错了什么?先谢谢你!
我从我的测试设备连接的图像,为您展示怪异敬酒结果是:
after this step, I show the new xmldata to a Toast, to check how it looks. However I receive part of the data, plus some weird characters. What I am doing wrong? Thank you in advance! I am attaching an image from my testing device, in order to show you the weird toast result:
您需要设置你的XML编码为UTF 8.你可以通过你的文件右击,然后设置文件编码从下拉这样做下去。
You need to set the encoding of your xml as UTF 8. You can do so by right clicking on your file and then set File Encoding from the drop down.