2013-08-30 46 views
0

我想获得两个HTML字符串之间的差异(字符串被删除并添加了字符串)。 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 "<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

你可以尝试考虑看看【JAVA的Diff-utils的(https://code.google.com/p/java-diff-utils/) – MadProgrammer

+0

@ØHankyPankyØ编后 –

+0

答案应该是“替换

  • foo
  • baar
  • AA
  • 在第1行第5列”。请参阅http://stackoverflow.com/a/3452129/120163 –

    回答

    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. 
        } 
    
    
        } 
    
    相关问题