2013-08-30 90 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. 
        } 
    
    
        } 
    
    相關問題