Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.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页面中删除html标记的最佳方法是什么?_Java_Html_Html Parsing - Fatal编程技术网

Java 从html页面中删除html标记的最佳方法是什么?

Java 从html页面中删除html标记的最佳方法是什么?,java,html,html-parsing,Java,Html,Html Parsing,从html页面中删除html标记的最佳方法是什么?我只想要实际的文本,而不是html标记。我将文本存储在字符串中,不包括html标记。最简单的方法是什么?示例页面如下所示: <HTML><HEAD> <META NAME="Docdate" CONTENT="05/02/2011"> <META NAME="m_title" CONTENT="TWO SECURITY GUARDS HACKED TO DEATH DURING A FIGHT">

从html页面中删除html标记的最佳方法是什么?我只想要实际的文本,而不是html标记。我将文本存储在字符串中,不包括html标记。最简单的方法是什么?示例页面如下所示:

<HTML><HEAD>
<META NAME="Docdate" CONTENT="05/02/2011">
<META NAME="m_title" CONTENT="TWO SECURITY GUARDS HACKED TO DEATH DURING A FIGHT">
<META NAME="m_author" CONTENT="">
<TITLE>MALAYSIA NEWS -- GENERAL NEWS -- 05/02/2011 -- TWO SECURITY GUARDS HACKED TO DEATH DURING A FIGHT</TITLE>
</HEAD><BODY BACKGROUND="#FFFFFF">
<PRE>
05/02/2011

POLICE-FIGHT

TWO SECURITY GUARDS HACKED TO DEATH DURING A FIGHT





    KUALA LUMPUR, Feb 5 (Bernama) -- Two security guards were hacked to death in

a fight that broke out at Damansara Perdana construction site last night. 

    Both men, aged 20 and 26, were found dead at the scene with slash wounds on

their bodies in the 8.20pm incident. 

    Petaling Jaya OCPD ACP Arjunaidi Mohammed said the fight started following

an argument involving a security guard and several foreign workers at the site. 

    "One of them had an argument with several of the workers. He then called two

of his friends who are also security guards but working in other areas. 

    "A group of 12 to 15 foreign workers, carrying sharp weapons, then attacked

them," he told reporters at the scene today. 

    The other security guard managed to flee to safety, he added. 

    "The foreign workers had also left the area. We have picked up a security

guard in the area and two Indonesian workers to have their statements taken," he

said, adding that a manhunt was underway for the suspects. 

    -- BERNAMA 

    NMR AKT JS





</PRE>
<BODY></HTML>

马来西亚新闻——综合新闻——2011年2月5日——两名保安在一场战斗中被砍死
05/02/2011
警斗
两名保安在一次搏斗中被砍死
吉隆坡,2月5日(伯纳马)--两名保安在一次袭击中被砍死
昨晚在Damansara Perdana建筑工地发生的一场斗殴。
两名年龄分别为二十及二十六岁的男子被发现当场死亡,身上有刀伤
他们的尸体在晚上8点20分的事件中。
Petaling Jaya OCPD ACP Arjunaidi Mohammed说,战斗开始于
涉及现场一名保安和几名外籍工人的争论。
“其中一人与几名工人发生了争执,然后打电话给两名工人
他的朋友也是保安,但在其他地区工作。
“一个由12至15名外籍工人组成的团体,携带锋利的武器,随后袭击
他今天在现场告诉记者。
他补充说,另一名保安设法逃到了安全地带。
“外国工人也已离开该地区。我们找到了一个保安
他说:“该地区的警卫和两名印尼工人要求对他们的证词进行记录。”
他补充说,目前正在追捕嫌疑人。
--伯纳玛
核磁共振AKT JS

使用类似于解析HTML的库,然后遍历生成的DOM并输出文本节点“通常是非常畸形的,所以尝试自己可靠地完成这项工作将是巨大的浪费。最好使用一个已经设计用于处理格式错误的HTML的库。

您看过jsoup吗?假设是一个球形的cow。另外,假设页面存储在名为
str
字符串中。假设您知道如何使用正则表达式。现在,使用
str.replaceAll()
删除所有html标记(如果您不知道,它是
str.replaceAll(“\\”,”)
。请注意,这只会删除包含在中的所有数据)不要尝试为此使用正则表达式。这将是一个可怕的错误。@Quincunx这是一个非常糟糕的主意。@user2310289我知道,但这可能是用正则表达式很容易得到的最好的结果。我绝对建议不要使用正则表达式。