英文:
Convert HTML to text in Java
问题
> All,
>
> Please find attached file for this week.
>
> Thanks,
> Support Team
英文:
I have a Java string like this:
String str = "<table><tr><td>ALL,</td></tr><tr><td></td></tr><tr><td> Please find attached file for this week<tr><td></td></tr><tr><td>Thanks</td></tr><tr><td>Support Team</td></tr>";
I want output like this:
> All,
>
> Please find attached file for this week.
>
> Thanks,
> Support Team
答案1
得分: 2
你应该真正使用一个合适的HTML解析器,但如果你想要一些快速且简单的方法并且你的HTML格式良好,你可以使用javax.swing.text.html
包中的内容:
public static String stripTags(String content) throws Exception {
String result = null;
HTMLEditorKit kit = new HTMLEditorKit();
InputStream in = new ByteArrayInputStream(content.getBytes());
Document doc = new HTMLDocument();
kit.read(in, doc, 0);
result = doc.getText(0, doc.getLength());
return result;
}
英文:
You should really use a proper html parser, but if you want something quick and dirty and your html is well-formed you can use something from package javax.swing.text.html
:
public static String stripTags(String content) throws Exception {
String result = null;
HTMLEditorKit kit = new HTMLEditorKit();
InputStream in = new ByteArrayInputStream(content.getBytes());
Document doc = new HTMLDocument();
kit.read(in, doc, 0);
result = doc.getText(0, doc.getLength());
return result;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论