Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的Sax解析器编码_Java_Android_Encoding_Rss_Xml Parsing - Fatal编程技术网

Java中的Sax解析器编码

Java中的Sax解析器编码,java,android,encoding,rss,xml-parsing,Java,Android,Encoding,Rss,Xml Parsing,我对sax解析器和编码文本有问题。我尝试以以下方式解析ISO-8859-2()中的RSS: InputStream responseStream = connection.getInputStream(); Response response = mRequest.createResponse(); Reader reader = new InputStreamReader(responseStream); InputSource is = new InputSource(reader); i

我对sax解析器和编码文本有问题。我尝试以以下方式解析ISO-8859-2()中的RSS:

InputStream responseStream = connection.getInputStream();
Response response = mRequest.createResponse();

Reader reader = new InputStreamReader(responseStream);
InputSource is = new InputSource(reader);
is.setEncoding("ISO-8859-2");

SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(is, response);
但解析器返回带有奇怪符号的字符串。我试了很多东西,但都没用:(有人能帮我吗


您是否尝试设置InputStreamReader的字符集:

Reader reader = new InputStreamReader(responseStream, Charset.forName("ISO-8859-2"));
InputSource is = new InputSource(reader);
如果不指定字符集,InputStreamReader(InputStream)构造函数将使用默认字符集(在我的计算机中是windows-1252)


因此,在您当前的设置中,字节被解释为(可能)windows-1252字符,在此之后,我认为您无法将其重新解释为ISO-8859-2。

最后,我使用解决了我的问题。它也适用于ISO-8859-2。以下是源代码,如何使用罗马:

String urlstring = "http://www.sbazar.cz/rss.xml?keyword=pes";
InputStream is = new URL(urlstring).openConnection().getInputStream();
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = (SyndFeed)input.build(new InputStreamReader(is, Charset.forName("ISO-8859-2")));

Iterator entries = feed.getEntries().iterator();
while (entries.hasNext())
{
    SyndEntry entry = (SyndEntry)entries.next();
    Log.d("RSS", "-------------");
    Log.d("RSS", "Title: " + entry.getTitle());
    Log.d("RSS", "Published: " + entry.getPublishedDate());

    if (entry.getDescription() != null) 
    {
        Log.d("RSS", "Description: " + entry.getDescription().getValue());
    }
    if (entry.getContents().size() > 0) 
    {
        SyndContent content = (SyndContent)entry.getContents().get(0);
        Log.d("RSS", "Content type=" + content.getType());
        Log.d("RSS", "Content value=" + content.getValue());
    }
} 

如果给定的是输入流,而不是读取器,Sax能够自动检测编码

InputSource is = new InputSource(responseStream)
可能在你的例子中,你想要一个硬编码编码,你得到了如何实现的答案。但我正在寻找一个通用的解决方案,并在这里找到了一个:

文档:(注意java 1.4文档缺少关键语句)。使用XML规范中的算法自动检测字符编码。该算法指的是字节流,而不是字符流(读取器)


当我在XML文档中挖掘更多信息时(),我找到了处理读卡器和流之间区别的解释。要应用所有编码算法,Sax必须能够访问原始流,而不是转换为字符,因为转换可能会损坏字节标记。

使用UTF-8试试,至少我的浏览器说明了编码是什么。或者您可能需要读取enc如果存在,则从响应头进行编码。您也可以在InputStreamReader中设置编码,可能需要使用两种UTF-8,但它仍然返回奇怪的符号。我还尝试在InputStreamReader中设置编码,但没有效果。响应头为:HTTP/1.1 200 OK Date:Mon,2012年3月26日20:19:21 GMT服务器:Apache:Accept Encoding内容类型:application/rss+xml Transfer Encoding:Chunked此代码原因:ParseException:在第1行第0列:格式不正确(无效标记)。xml文件是否有有效的xml头:new InputStreamReader(is,Charset.forName(“ISO-8859-2”)-我说的差不多了?