Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.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_Diff - Fatal编程技术网

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

Java 获取两个html字符串之间的差异,java,diff,Java,Diff,我想得到两个HTML字符串之间的差异(删除字符串和添加字符串)。差分函数的函数必须给出如下结果: 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 "&

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

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>";
}
String html1=“foo-foo”;
字符串html2=“foo-baar”;
私有字符串差异(字符串html1、字符串html2){
...
//diff方法应返回以下内容:
返回“foo foo baar”;
}
我尝试了diff_match_补丁,但它与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>
String html1=“
  • foo
  • baar
  • ” 字符串html2=“
  • AA
  • ” diff_match_patch(html1,html2)提供以下diff字符串:
  • AA
  • BB
  • 应该是:

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

  • 即使您告诉我们它们是Html字符串,但对于java来说,它们与任何其他普通的
    字符串一样

    试一试


    你可以试着看一下@ØHankyPankyØpost edited,答案应该是,“在第1行第5列将
  • foo
  • baar
  • 替换为
  • AA
  • ”。看见
        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.
      }
    
    
        }