如何在Java中修改Html内容
假设我有下面的html字符串。每个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;\">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> bbb </p>\n </div> </div>
如果html只包含一个div,那么这就是需要修改的最后一个div。请让我知道如何在Java中实现这一点
# 1 楼答案
我假设您的主要目标是将数据存储到数据库中,并使其动态用于各种目的/元素
步骤1)首先获取DIV并将其存储到变量中
步骤2)基于模式将DIV转换为XML/JSON(包括数据类型)。不要忘记处理失败场景
步骤3)应用您的业务逻辑或任何其他修改
步骤4)使用XML路径或Json路径获取要存储的数据,并对其进行迭代
所有这些都可以通过java代码实现