使用 document.parse(string) 时获取 SAXException
我想重用来自 HTTP response.resultset() 的输入流...
- 我将输入流转换为 byte[] 数组(还需要存储,因此我创建字节数组。)
- 而不是
- 在将其传递给 document.parse( string) 给出了错误 saxparserException:---eof not find
- 与 document.parse(stream) 一起正常工作。
//------------以下方法对我没有帮助,每个方法都会导致
saxparserException 协议未找到。
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.IOException;
public static String readInputStreamAsString(InputStream in)
throws IOException {
BufferedInputStream bis = new BufferedInputStream(in);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result = bis.read();
while(result != -1) {
byte b = (byte)result;
buf.write(b);
result = bis.read();
}
return buf.toString();
}
//--------------
byte[] bytes=new byte[inputStream.available()];
inputStream.read(bytes);
String s = new String(bytes);
//------------------
StringBuilder response = new StringBuilder();
int value = 0;
boolean active = true;
while (active) {
value = is.read();
if (value == -1) {
throw new IOException("End of Stream");
} else if (value != '\n') {
response.append((char) value);
continue;
} else {
active = false;
}
}
return response.toString();
//--------------
BufferedInputStream ib = new BufferedInputStream(is,1024*1024);
StringBuffer sb = new StringBuffer();
String temp = ib.readLine();
while (temp !=null){
sb.append (temp);
sb.append ("\n");
temp = ib.readLine();
}
s = sb.toString()
I want to reuse the Inputstream coming from HTTP response.resultset()...
- I converted inputstream to byte[] array (also need to store so I create byte array.)
- Than convert into string
- when i pass it to document.parse(string) gave error saxparserException:---eof not found
- working fine with document.parse(stream).
//------------Following methods are not helpfull for me ,each of them leads to
saxparserException protocl not found .
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.IOException;
public static String readInputStreamAsString(InputStream in)
throws IOException {
BufferedInputStream bis = new BufferedInputStream(in);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result = bis.read();
while(result != -1) {
byte b = (byte)result;
buf.write(b);
result = bis.read();
}
return buf.toString();
}
//--------------
byte[] bytes=new byte[inputStream.available()];
inputStream.read(bytes);
String s = new String(bytes);
//------------------
StringBuilder response = new StringBuilder();
int value = 0;
boolean active = true;
while (active) {
value = is.read();
if (value == -1) {
throw new IOException("End of Stream");
} else if (value != '\n') {
response.append((char) value);
continue;
} else {
active = false;
}
}
return response.toString();
//--------------
BufferedInputStream ib = new BufferedInputStream(is,1024*1024);
StringBuffer sb = new StringBuffer();
String temp = ib.readLine();
while (temp !=null){
sb.append (temp);
sb.append ("\n");
temp = ib.readLine();
}
s = sb.toString()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我通过使用解决了这个问题:-
document.parse(new ByteArrayInputStream(stream))
I solved the issue by using:-
document.parse(new ByteArrayInputStream(stream))
如果您收到包含 xml 数据的 http 响应,则可以使用 saxparser 解析 xml 数据。
If you got http response which includes xml data, you can parse xml data using saxparser.
我怀疑您可能误解了
SAXParser.parse
方法签名。采用 String 参数的 parse 方法版本期望 String 表示引用内容的 URI,而不是内容本身。
如果您已将输入保存在 String 变量中,则最好在字符串上打开 StringReader,将 InputSource 基于 StringReader,然后将其传递给解析器。
I suspect you may have misunderstood the
SAXParser.parse
method signature.The versions of the parse method that take a String argument expect that String to represent a URI referring to the content and not the content itself.
If you've saved your input in a String variable, you're best off opening a StringReader on the string, basing an InputSource on the StringReader, and passing that to the parser.