投稿する前に、私は多くのトピックを検索しましたが、それはとても単純な問題のようです. しかし、私は問題を理解していませんでした。
シナリオは基本的なものです: HTTP 接続を介してリモート コンピューターから XML を解析したい:
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
try {
URL url = new URL("http://host:port/file.xml");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept","application/xml");
InputStream is = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
PrintWriter pw = new PrintWriter("localfile_pw.xml");
FileOutputStream fos = new FileOutputStream("localfile_os.xml");
次に、XMLを読み取る3つの異なる方法を試しました
バイトストリームの読み取り
byte[] buffer = new byte[4 * 1024];
int byteRead;
while((byteRead= is.read(buffer)) != -1){
fos.write(buffer, 0, byteRead);
}
1 文字あたりの読み取り文字数
char c;
while((c = (char)br.read()) != -1){
pw.print(c);
System.out.print(c);
}
行ごとに読む
String line = null;
while((line = br.readLine()) != null){
pw.println(line);
System.out.println(line);
}
すべての場合において、xml の読み取りは、まったく同じバイト数の後、同じ時点で停止します。そして、読んだり、例外を与えたりせずに立ち往生します。
前もって感謝します。