Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将HTML文本转换为纯文本?_Java_Html - Fatal编程技术网

Java 如何将HTML文本转换为纯文本?

Java 如何将HTML文本转换为纯文本?,java,html,Java,Html,朋友的 我必须从url解析描述,解析后的内容几乎没有html标记,因此如何将其转换为纯文本 htmlString=htmlString.replaceAll("\\<.*?\\>", ""); String plainText= Jsoup.parse(yout_html_text).text(); 详细答案:我建议您解析原始HTML,通过它您可以编写xpath表达式。这是我发现的最有效的清除HTML的方法。清除HTML标记很简单: // replace all occurren

朋友的 我必须从url解析描述,解析后的内容几乎没有html标记,因此如何将其转换为纯文本

htmlString=htmlString.replaceAll("\\<.*?\\>", "");
String plainText= Jsoup.parse(yout_html_text).text();

详细答案:

我建议您解析原始HTML,通过它您可以编写xpath表达式。这是我发现的最有效的清除HTML的方法。

清除HTML标记很简单:

// replace all occurrences of one or more HTML tags with optional
// whitespace inbetween with a single space character 
String strippedText = htmlText.replaceAll("(?s)<[^>]*>(\\s*<[^>]*>)*", " ");
//将所有出现的一个或多个HTML标记替换为可选标记
//中间带空格的单个空格字符
String strippedText=htmlText.replaceAll(“(?s)]*>(\\s*]*>)*”,“”);
但不幸的是,要求从来没有这么简单:


通常,
元素需要单独处理,可能有带有
字符(例如javascript)的cdata块会弄乱正则表达式等。

您可以使用这一行删除html标记并将其显示为纯文本

htmlString=htmlString.replaceAll("\\<.*?\\>", "");
String plainText= Jsoup.parse(yout_html_text).text();
htmlString=htmlString.replaceAll(“\\”,”);

如果要分析类似于浏览器显示的内容,请使用:

import net.htmlparser.jericho.*;
import java.util.*;
import java.io.*;
import java.net.*;

public class RenderToText {
    public static void main(String[] args) throws Exception {
        String sourceUrlString="data/test.html";
        if (args.length==0)
          System.err.println("Using default argument of \""+sourceUrlString+'"');
        else
            sourceUrlString=args[0];
        if (sourceUrlString.indexOf(':')==-1) sourceUrlString="file:"+sourceUrlString;
        Source source=new Source(new URL(sourceUrlString));
        String renderedText=source.getRenderer().toString();
        System.out.println("\nSimple rendering of the HTML document:\n");
        System.out.println(renderedText);
  }
}
我希望这将有助于以浏览器格式解析表

谢谢,
Ganesh

我需要一些HTML的纯文本表示,其中包括FreeMarker标记。这个问题是通过JSoup解决方案交给我的,但是JSoup正在逃避FreeMarker标记,从而破坏了功能。我还尝试了htmlCleaner(sourceforge),但这留下了HTML标题和样式内容(标记被删除)。

我的代码:

return new net.htmlparser.jericho.Source(html).getRenderer().setMaxLineLength(Integer.MAX_VALUE).setNewLine(null).toString();
maxLineLength
确保行不会被人为地包装为80个字符。
setNewLine(null)
使用与源代码相同的新行字符。

是,将是更好的选择。只需像下面这样将整个HTML文本转换为纯文本

htmlString=htmlString.replaceAll("\\<.*?\\>", "");
String plainText= Jsoup.parse(yout_html_text).text();

我使用
HTMLUtil.textFromHTML(值)


org.clapper
javautil
3.2.0
使用Jsoup

添加依赖项

<dependency>
  <!-- jsoup HTML parser library @ https://jsoup.org/ -->
  <groupId>org.jsoup</groupId>
  <artifactId>jsoup</artifactId>
  <version>1.13.1</version>
</dependency>

只需调用传递html文本的方法html2text,它将返回纯文本。

使用Jsoup,我得到了同一行中的所有文本

htmlString=htmlString.replaceAll("\\<.*?\\>", "");
String plainText= Jsoup.parse(yout_html_text).text();
因此,我使用以下代码块解析HTML并保留新行:

private String parseHTMLContent(String toString) {
    String result = toString.replaceAll("\\<.*?\\>", "\n");
    String previousResult = "";
    while(!previousResult.equals(result)){
        previousResult = result;
        result = result.replaceAll("\n\n","\n");
    }
    return result;
}
private String parseHTMLContent(字符串到字符串){
字符串结果=toString.replaceAll(“\\”,“\n”);
字符串previousResult=“”;
而(!previousResult.equals(result)){
先前的结果=结果;
result=result.replaceAll(“\n\n”和“\n”);
}
返回结果;
}

不是最好的解决方案,但解决了我的问题:)

您的确切要求是什么?你需要去掉HTML标签吗?提取特定标记的内容?我可以提取内容,但内容有zcc-dsdfsf-ddfdfsf

sfdfdfdff,如上所述,我正在获取数据,但我需要是一个简单的纯文本。没有这些html标记,类似的问题在这里有很好的答案:。我用的是杰里科,效果很好。你应该把这个问题标记为已回答。重复的,,和,那些被否决的投票者能解释他们为什么被否决吗?关于为什么这在一般情况下不起作用,并且不会是f(u | oo)l证明:爱它。。。非常简单,但又非常强大,现在还可以使用
Jsoup.parse(html).wholeText()