获取两个html字符串之间的差异

时间:2013-08-30 06:56:37

标签: java diff

我想在两个HTML字符串之间获得差异(删除字符串和添加字符串)。 diff函数的功能必须给出如下结果:

String html1 = "<h1>foo foo </h1>";
String html2 = "<h1>foo baar </h1>";

private String diff(String html1, String html2){
 ...
// diff method should return following:
return "<h1>foo <span class = "deleted">foo</span> <span class = "added">baar </span> </h1>";
}

我尝试过diff_match_patch,但它有html标签问题。示例:

String html1 = "<ol><li>foo</li><li>baar</li></ol>" 
String html2 = "<ol><li>AA</li></ol>"

diff_match_patch(html1, html2) gives the following diff string:

<ol>
<li>AA<del style="background:#ffe6e6;"></li>
<li>BB</del>
</li>
</ol>

它应该是:

<ol>
<li>AA</li>
<del style="background:#ffe6e6;"><li>BB</del>
</li>
</ol>

1 个答案:

答案 0 :(得分:0)

即使你告诉他们是Html字符串,对于java,它们就像任何其他正常的String一样。

尝试

    private String diff(String html1, String html2){

    // trim the string and null checks ..etc
    if(html1.equalsIgnoreCase(html2)){

     //  string  are same 
     }

    else {
      // they are different.
  }


    }