在android中处理一个xml文件后,出现了奇怪的字符 [英] Weird characters after processing a xml file in android

查看:107
本文介绍了在android中处理一个xml文件后,出现了奇怪的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个从一个xml文件加载数据的安卓的应用程序。问题是xml文件没有正确处理,我无法序列化它,因为这样。我认为问题与文件的编码有关。



我如何制作xml文件?从Eclipse我点击了New-> File - >并创建了一个空白的xml,然后我填写了所需的信息。



这是xml的外观在编辑器中:

 <?xml version =1.0encoding =UTF-8?> 
< data>
< categories>
< category value =Inbox/>
< category value =Private/>
< category value =Work/>
< category value =Business/>
< / categories>

< todos>
< todo>
< id> 1< / id>
< text>浏览应用!< / text>
< / todo>

< todo>
< id> 2< / id>
< text>添加更多todos!< / text>
< date> 2013-05-09 12:21:55 CET< / date>
< / todo>
< / todos>
< / data>

我从res / xml获取xml文件,然后将文件加载到输入流中。之后,我使用String构造函数将文件转换为String。



这是java代码:

  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显示给Toast,以查看它的外观。但是我收到了部分数据,再加上一些奇怪的字符。我做错了什么?先谢谢你!
我附加了我的测试设备的图像,以便向您显示奇怪的烤面包机的结果:



解决方案

您需要设置编码的xml为UTF 8.您可以通过右键单击文件,然后从下拉列表中设置文件编码。


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.

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.

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>

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.

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");

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:

解决方案

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.

这篇关于在android中处理一个xml文件后,出现了奇怪的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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