将HTML转换为Java中的纯文本

前端之家收集整理的这篇文章主要介绍了将HTML转换为Java中的纯文本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要将 HTML转换为纯文本.我唯一的格式化要求是在纯文本中保留新行.应该显示新的行,不仅在< br>但其他标签,例如< tr />,< / p为H.也导致了一条新线. 用于测试的HTML页面示例如下:“http://www.particle.kth.se/~lindsey/ JavaCourse/Book/Part1/Java/Chapter09/scannerConsole.html”或“http://www.javadb.com/write -to-file-using-bufferedwriter“.请注意,这些只是随机的网址.

我已经尝试了以下线程中提到的将HTML转换为纯文本的各种库(JSoup,Javax.swing,Apache utils).
参考:“https://stackoverflow.com/questions/240546/removing-html-from-a-java-string”

使用JSoup的示例:

public class JSoupTest {

 @Test
 public void SimpleParse() {
  try {
   Document doc = Jsoup.connect("http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter09/scannerConsole.html").get();
   System.out.print(doc.text());

  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

HTMLEditorKit示例:

import javax.swing.text.html.*;
import javax.swing.text.html.parser.*;

public class Html2Text extends HTMLEditorKit.ParserCallback {
 StringBuffer s;

 public Html2Text() {}

 public void parse(Reader in) throws IOException {
   s = new StringBuffer();
   ParserDelegator delegator = new ParserDelegator();
   // the third parameter is TRUE to ignore charset directive
   delegator.parse(in,this,Boolean.TRUE);
 }

 public void handleText(char[] text,int pos) {
   s.append(text);
 }

 public String getText() {
   return s.toString();
 }

 public static void main (String[] args) {
   try {
     // the HTML to convert
    URL  url = new URL("http://www.javadb.com/write-to-file-using-bufferedwriter");
    URLConnection conn = url.openConnection();
    BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
    String inputLine;
    String finalContents = "";
    while ((inputLine = reader.readLine()) != null) {
     finalContents += "\n" + inputLine.replace("<br","\n<br");
    }
    BufferedWriter writer = new BufferedWriter(new FileWriter("samples/testHtml.html"));
    writer.write(finalContents);
    writer.close();

     FileReader in = new FileReader("samples/testHtml.html");
     Html2Text parser = new Html2Text();
     parser.parse(in);
     in.close();
     System.out.println(parser.getText());
   }
   catch (Exception e) {
     e.printStackTrace();
   }
 }
}

解决方法

让您的解析器将文本内容和换行符附加到StringBuilder.
final StringBuilder sb = new StringBuilder();
HTMLEditorKit.ParserCallback parserCallback = new HTMLEditorKit.ParserCallback() {
    public boolean readyForNewline;

    @Override
    public void handleText(final char[] data,final int pos) {
        String s = new String(data);
        sb.append(s.trim());
        readyForNewline = true;
    }

    @Override
    public void handleStartTag(final HTML.Tag t,final MutableAttributeSet a,final int pos) {
        if (readyForNewline && (t == HTML.Tag.DIV || t == HTML.Tag.BR || t == HTML.Tag.P)) {
            sb.append("\n");
            readyForNewline = false;
        }
    }

    @Override
    public void handleSimpleTag(final HTML.Tag t,final int pos) {
        handleStartTag(t,a,pos);
    }
};
new ParserDelegator().parse(new StringReader(html),parserCallback,false);
原文链接:https://www.f2er.com/html/224956.html

猜你在找的HTML相关文章