保存/读取大型xml字符串到/从文件

时间:2013-03-27 16:02:05

标签: android xml file save

所以,我的应用程序从soap服务器收到一个大的xml。我希望将其保存在一个文件中,以备日后使用。我设法做到这一点,并阅读文件。但结果(读完后)是乱码xml!来自xml后半部分的大部分文本(412个字符)被复制并粘贴到我的xml的末尾,我无法弄清楚为什么会发生这种情况。  我已经尝试了2种方法来编写文件和2种方法来读取文件,没有骰子! (将在下面发布代码)注意:xml是5000-20000个大字符,因此我使用方法来防止eclipse返回内存不足错误。

BOTTOM LINE:

- 输入xml文件正确

-output xml文件不正确

-tried 2 save methods

-tried 2 read methods

-wtf?!

保存代码1:

                    InputStream is = new ByteArrayInputStream(string.getBytes());

                    FileOutputStream fos = ctx.openFileOutput(filename, Context.MODE_PRIVATE);

                        byte[] buffer = new byte[1024];
                        int length;
                        while ((length = is.read(buffer))>0){
                            fos.write(buffer, 0, length);
                        }

                        fos.flush();
                        fos.close();
                        is.close();

保存代码2:

                InputStream is = new ByteArrayInputStream(string.getBytes());

                    BufferedReader reader = new BufferedReader(new InputStreamReader(
                            is, "iso-8859-1"), 8);
                    FileOutputStream fos = ctx.openFileOutput(filename, Context.MODE_PRIVATE);
                    StringBuilder sb = new StringBuilder();
                    String line = null;

                    while ((line = reader.readLine()) != null) {
                        Log.e("stuff is good", "line: "+line);
                        sb.append(line);

                        if (sb.toString().length() > 10000) {
                           fos.write(sb.toString().getBytes());
                           fos.flush();
                           sb = new StringBuilder();

                        }

                    }

                    fos.write(sb.toString().getBytes());
                    fos.flush();
                    is.close();
                    fos.close();

阅读代码1:

            FileInputStream fis = openFileInput("caca");
                    int c;
                    StringBuilder fileContent = new StringBuilder();

                    while((c=fis.read())!=-1)
                            {
                        fileContent.append((char)c);

                            }
                    fis.close();

                    Log.e("TEST TEST", "XML length = "
                            +String.valueOf(fileContent.length()) );
                    Log.e("TEST TEST", "XML = "
                            +fileContent );

阅读代码2:

        FileInputStream fis;
                    fis = openFileInput("caca");
                    StringBuffer fileContent = new StringBuffer("");
                    byte[] buffer = new byte[1024];
                    int i =1;
                    while (fis.read(buffer) != -1) {
                        fileContent.append(new String(buffer));
                        Log.v("TEST"+ String.valueOf(i), new String(buffer) );
                        i++;
                    }
                    Log.e("TEST TEST", "XML length = "
                            +String.valueOf(fileContent.length()) );
                    Log.e("TEST TEST", "XML = "
                            +fileContent );

保存到文件代码:

                        File myFile = new File("/sdcard/mysdfile.txt");
                        myFile.createNewFile();
                        FileOutputStream fOut = new FileOutputStream(myFile);
                        OutputStreamWriter myOutWriter = 
                                                new OutputStreamWriter(fOut);
                        myOutWriter.append(fileContent);
                        myOutWriter.close();
                        fOut.close();
                        Toast.makeText(getBaseContext(),
                                "Done writing SD 'mysdfile.txt'",
                                Toast.LENGTH_SHORT).show();

很抱歉很长的帖子,但是3天后,我的智慧结束了。任何输入都会很好,谢谢!

2 个答案:

答案 0 :(得分:0)

我更喜欢使用Apache Commons IO

DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url + id);

try {
    HttpResponse response = client.execute(httpGet);
    InputStream content = response.getEntity().getContent();        
    StringWriter writer = new StringWriter();
    IOUtils.copy(content, writer, "utf-8");
    return writer.toString(); 

} catch (ClientProtocolException e) {
    Log.e(tag, "client problem:" + e.getMessage());
    throw new RuntimeException("client problem",e);
} catch (IOException e) {
    Log.e(tag, "IO problem:" + e.getMessage());
    throw new RuntimeException("IO problem",e);
}

然后像往常一样写出字符串。

答案 1 :(得分:0)

好吧....我修好了,我不知道为什么会这样。

保存代码:

    public static void Save(String filename, String string, 
          Context ctx) {
            Log.e("stuff is good", "xml length b4 save= "+String.valueOf(string.length()));

                try {
                    FileOutputStream fOut = ctx.openFileOutput(filename, Context.MODE_PRIVATE); 

                    OutputStreamWriter myOutWriter = 
                                            new OutputStreamWriter(fOut);
                    myOutWriter.append(Login.messagesXmlDump);
                    myOutWriter.close();
                    fOut.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

阅读代码:

                    Save("LOL", messagesXmlDump, getApplicationContext());

                try {

                    FileInputStream fis = openFileInput("LOL");
                    int c;
                    StringBuilder fileContent = new StringBuilder();

                    while((c=fis.read())!=-1)
                            {
                        fileContent.append((char)c);

                            }
                    fis.close();

管理写/读长70k字符的xml。也许这种保存它的递归方法不是最好的主意。认为我过于复杂化了一件简单的事情。

抱歉浪费你的时间:(