0

リモート http サーバーで、Sublime text2 で test.xml ファイルを作成し、エンコード utf-8 で保存します。

<?xml version='1.0' encoding='Utf-8' ?>
<Shops>
 <Shop name="one"></Shop>
 <Shop name="two" ></Shop>
 <Shop name="three"></Shop>
</Shops>

次に、デバイスにダウンロードします。

String str="";
            URL url = new URL(Server+urls);
            URLConnection ucon = url.openConnection();
            InputStream is = ucon.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayBuffer baf = new ByteArrayBuffer(50);
            int current = 0;
            while ((current = bis.read()) != -1) 
            {
                baf.append((byte) current);
            }
            str = new String(baf.toByteArray(),"Utf-8");
            DataOutputStream out = null;
            out = new DataOutputStream(openFileOutput(filename, Context.MODE_PRIVATE));
            out.writeUTF(str);
            out.close();

その後、DDMS ファイル エクスプローラーを使用して Macbook にダウンロードし、Sublime text2 で開き、次のように表示されました。

008e 3c3f 786d 6c20 7665 7273 696f 6e3d
2731 2e30 2720 656e 636f 6469 6e67 3d27
5574 662d 3827 203f 3e0d 0a3c 5368 6f70
733e 0d0a 203c 5368 6f70 206e 616d 653d
226f 6e65 223e 3c2f 5368 6f70 3e0d 0a20
3c53 686f 7020 6e61 6d65 3d22 7477 6f22
203e 3c2f 5368 6f70 3e0d 0a20 3c53 686f
7020 6e61 6d65 3d22 7468 7265 6522 3e3c
2f53 686f 703e 0d0a 3c2f 5368 6f70 733e

次に、utf-8 をエンコードして再度開くことを選択し、見ました (ちなみに、私が見たものをコピー/貼り付けすることはできません):

ここに画像の説明を入力

4

2 に答える 2

0

.writeUTFUTF-8ではなく、変更された UTF-8を使用します。

これ0x00 0x8eは、XML の長さを表す符号なしのビッグ エンディアンです。これは 142 であり、実際の長さである 142 にも一致します。

これを使って:

str = new String(baf.toByteArray(),"UTF-8");
OutputStreamWriter osw = new OutputStreamWriter(
    openFileOutput(filename, Context.MODE_PRIVATE),
    Charset.forName("UTF-8").newEncoder()
);
osw.write(str);
于 2013-01-17T16:48:12.563 に答える
0

バイトから/への変換を行っている理由はよくわかりませんが、この問題はStAXの良い候補かもしれません。

読み書きのセクションをチェックアウトします。

于 2013-01-17T15:37:08.340 に答える