乱码问题总算解决了

下面这段代码用来获取文章内容,并通过NekoHTML来解析获得去掉HTML标签的文章内容.标红的地方就是用来设置字符集的,第一个是XML格式的字符集(似乎没什么用),第二个地方是将字符串的内容通过输入流读入,如果不指定的话在GAE中默认的是ISO-8859-1(本地的话以设置的文件的字符类型为主),第三个地方是设置XML解析器的字符集。昨晚就是第二个地方没有设置,导致乱码。在测试的过程中还学到一点:GBK->ISO-8859-1 的过程是不可逆的,也就是说如果把中文字符转成了ISO-8859-1的话,就再也转不过来了,中文变成了"????"。因此在保险起见,输入输出流在使用的时候最好都加上字符集。
1     public String getContent(String xwnr) throws Exception {
2         String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><content>" + xwnr + "</content>";
3         DOMFragmentParser parser = new DOMFragmentParser();
4         DocumentFragment node = new HTMLDocumentImpl().createDocumentFragment();
5                
6         InputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8"));
7        
8         InputSource input = new InputSource(is);       
9         input.setEncoding("UTF-8");
10         try {
11             parser.parse(input, node);
12         } catch (IOException e) {
13             e.printStackTrace();
14         } catch (SAXException se) {
15             se.printStackTrace();
16         }
17         StringBuffer newContent = new StringBuffer();
18         this.getText(newContent, node);
19
20         /*String str  =  ( new  String(
21                 newContent.toString().getBytes("Windows-1252"),  "UTF-8" ));*/       
22        
23         String str = newContent.toString();
24        
25         if (str.length()>200){
26             return str.substring(0,200);
27         }else{
28             return str;
29         }       
30     }
今天受到了不少关注,非常高兴,非常感谢支持我的同学们,我会慢慢的将开发的过程写出来与大家分享。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值