Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.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,假设我有下面的html字符串。每个div由用户名、时间、日期和注释组成。我需要将此内容保存在db中,但在此之前,我需要用新的日期和时间修改最后一个div的日期和时间 <div class=\"customclass recordwidget comments-section\" style=\"margin-bottom:8px; background-color:#f8f8f8; border-bottom:1px solid #f01928;border-top:1px solid

假设我有下面的html字符串。每个div由用户名、时间、日期和注释组成。我需要将此内容保存在db中,但在此之前,我需要用新的日期和时间修改最后一个div的日期和时间

<div class=\"customclass recordwidget comments-section\" style=\"margin-bottom:8px;  background-color:#f8f8f8; border-bottom:1px solid #f01928;border-top:1px solid #f01928; padding:8px; line-height:1.1;\"><div class=\"headingcontent\"><b style=\"font-size:15px;\">Rob</b> <b>2/9/2018</b> 8:43:5 </div> <div class=\"recordwidget-content\"><p>Comments to modified date</p>\n </div> </div><div class=\"customclass recordwidget comments-section\" style=\"margin-bottom:8px;  background-color:#f8f8f8; border-bottom:1px solid #f01928;border-top:1px solid #f01928; padding:8px; line-height:1.1;\"><div class=\"headingcontent\"><b style=\"font-size:15px;\"> om</b> <b>4/25/2018</b> 8:8:32 </div> <div class=\"recordwidget-content\"><p>Hello World</p>\n </div> </div><div class=\"customclass recordwidget comments-section\" style=\"margin-bottom:8px;  background-color:#f8f8f8; border-bottom:1px solid #f01928;border-top:1px solid #f01928; padding:8px; line-height:1.1;\"><div class=\"headingcontent\"><b style=\"font-size:15px;\"> Perry</b> <b>4/26/2018</b> 4:1:58 </div> <div class=\"recordwidget-content\"><p>&nbsp; &nbsp; bbb&nbsp; &nbsp; &nbsp;&nbsp;</p>\n </div> </div>
Rob 2018年2月9日8:43:5对修改日期的评论

\n 2018年4月25日8:8:32你好世界

\n Perry 2018年4月26日4:1:58bbb

\n

如果html只包含一个div,那么这就是需要修改的最后一个div。请告诉我如何在Java中实现这一点。

我认为您的主要目标是将数据存储到数据库中,并使其动态用于各种目的/元素

步骤1)首先获取DIV并将其存储到变量中

步骤2)基于模式将DIV转换为XML/JSON(包括数据类型)。不要忘记处理失败场景

步骤3)应用您的业务逻辑或任何其他修改

步骤4)使用XML路径或Json路径获取要存储的数据,并对其进行迭代


所有这些都可以通过java代码实现

您可以使用HTML解析器库,如。非常广泛的问题。我会尝试一些像dom4j这样的lib;元素e=doc.getElementsByClass(“headingcontent”).last();如果(e.text().contains(“4/26/2018”){//找到了您想要的e.text().replaceAll(“4/26/2018”,“4/26/2018”);}请尝试:
doc.getElementsByClass(“headingcontent”).last().getElementsByTag(“b”).last()