RT,我想做一个将html文件转换为text文本的程序.
我写了如下代码:
import javax.swing.text.*;
import javax.swing.text.html.*;
import javax.swing.text.html.parser.*;
import java.io.*;
public class ParserTest{
public static void main(String[] args)throws Throwable{
Reader r =new StringReader( " <html> <body> A B </body> </html> ");
ParserCallback pc =new ParserCallback(System.out);
HTMLEditorKit.Parser p =new ParserGetter().getParser();
p.parse(r,pc,true);
}
}
class ParserCallback extends HTMLEditorKit.ParserCallback{
private PrintStream ps;
public ParserCallback(PrintStream ps){
this.ps =ps;
}
public void handleText(char[] text,int position){
ps.println(text);
}
}
class ParserGetter extends HTMLEditorKit{
//purely to make this method public
public HTMLEditorKit.Parser getParser(){
return super.getParser();
}
}
但是程序的输出是:
A??B
但我期望的应该是:
A B
谁能告诉我这是为什么?如何解决?
或者有其他更好的方法实现我的需求?
------解决方案--------------------
up