简体   繁体   中英

Parsing InputStream from URL StAX

I am trying to parse RSS feed, and have problem with encoding

if encoding utf-8, result correct, but problem with another type, espessially windows-1251

the code is below

 XMLInputFactory inputFactory = XMLInputFactory.newInstance();
 InputStream in = new URL(channel.getUrl()).openStream();;
 XMLEventReader eventReader = inputFactory.createXMLEventReader(in);

I don't want save a content to locale file, after read. Can anybody help?

It is very difficult to guess the encoding by just analyzing the bytes from the input stream. Therefore normally, the platform's default encoding is used, when you dio not specify it.

However, an XMLInputFactory can create XMLEventReader that uses a specific encoding. Just call the method XMLInputFactory.createXMLEventReader(InputStream stream, String encoding) .

That means, you must know the encoding before. Maybe there is a contract for the interface you are serving.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM