title: Java : Compare, mark and interpret HTML texts in Java
body: <p>I am working on a Java project, where there is an HTML editor and the user can enter text in a html-editor(ckeditor) and the actual HTML text is saved in the database.</p>
  
  <p>Now when a user comes again next time, and edits the same text, I would like to show the difference between the two by comparing it from the database.</p>
  
  <p>The most important problem I am facing is, even if any  comparator-tool knows that style from Italic has changed to Bold, the output of the comparator is it <code>strike-throughs</code> the word <code>Italic</code> and shows <code>Bold</code> was inserted in place of that. </p>
  
  <p>But that doesn't explain the <strong>Intention</strong> or <strong>Action</strong> of the actual edit. The intention/action was that the user made it from Italic to Bold. What I am looking for is a tool, which instead of showing that the word Italic was removed and Bold was added in place of that, would show me the <code>Italic</code> word/sentence first which is strikethrough and the replacement by the <code>Bold</code>  word/sentence. </p>
  
  <p>I hope what I mean is clear. I have been trying to achieve this for quite some time. I tried diff_match_patch, daisydiff, etc, nothing helped.</p>
  
  <p>My trials :</p>
  
  <pre><code>/*
  
              String oldTextHtml = mnotes1.getMnotetext();
              String newTextHTML = mnotes.getMnotetext();
  
  
              oldTextHtml = oldTextHtml.replace("&lt;br&gt;","\n");
              oldTextHtml = Jsoup.clean(oldTextHtml, Whitelist.basic());
             oldTextHtml = Jsoup.parse(oldTextHtml).text();
  
              newTextHTML = newTextHTML.replace("&lt;br&gt;","\n");
              newTextHTML = Jsoup.clean(newTextHTML,Whitelist.basic());
              newTextHTML = Jsoup.parse(newTextHTML).text();
  
  
              diff_match_patch diffMatchPatch = new diff_match_patch();
              LinkedList&lt;diff_match_patch.Diff&gt; deltas = diffMatchPatch.diff_main(oldTextHtml, newTextHTML);
              diffMatchPatch.diff_cleanupSemantic(deltas);
              newText += diffMatchPatch.diff_prettyHtml(deltas);
              groupNoteHistory.setWhatHasChanged("textchange");
              groupNoteHistory.setNewNoteText(newText);
              noEdit = true;
  */
  
  
             List&lt;String&gt; oldTextList = Arrays.asList(mnotes1.getMnotetext().split("(\\.|\\n)"));
              List&lt;String&gt; newTextList = Arrays.asList(mnotes.getMnotetext().split("(\\.|\\n)"));
              if (oldTextList.size() == newTextList.size()) {
  
                  for (int current = 0; current &lt; oldTextList.size(); current++) {
                      if (isLineDifferent(oldTextList.get(current), newTextList.get(current))) {
                          noEdit = true;
                          diff_match_patch diffMatchPatch = new diff_match_patch();
                          LinkedList&lt;diff_match_patch.Diff&gt; deltas = diffMatchPatch.diff_main(oldTextList.get(current), newTextList.get(current));
                          diffMatchPatch.diff_cleanupSemantic(deltas);
                          newText += diffMatchPatch.diff_prettyHtml(deltas);
                          groupNoteHistory.setWhatHasChanged("textchange");
                          groupNoteHistory.setNewNoteText(newText);
                      }
                  }
              } else {
                  if (!(mnotes.getMnotetext().equals(mnotes1.getMnotetext()))) {
                      if (isLineDifferent(mnotes1.getMnotetext(), mnotes.getMnotetext())) {
                          diff_match_patch diffMatchPatch = new diff_match_patch();
  
                          LinkedList&lt;diff_match_patch.Diff&gt; deltas = diffMatchPatch.diff_main(mnotes1.getMnotetext(),
                                  mnotes.getMnotetext());
                          diffMatchPatch.diff_cleanupSemantic(deltas);
                          newText += diffMatchPatch.diff_prettyHtml(deltas);
                          groupNoteHistory.setWhatHasChanged("textchange");
                          noEdit = true;
                      }
                      groupNoteHistory.setNewNoteText(newText);
                      groupNoteHistory.setWhatHasChanged("textchange");
                  }
              }
  </code></pre>
  
  <p>If anyone has any idea how I can achieve this, kindly let me know. Thanks a lot. :-)</p>
  
  <p><em>Edit</em></p>
  
  <p>I was asked for an image. Explanation and then the image.</p>
  
  <pre><code>Old text : &lt;style= bold&gt;Hello&lt;/style&gt;
  new Text : &lt;style = Italic&gt;Hello&lt;/style&gt;
  </code></pre>
  
  <p>Difference output expected :</p>
  
  <p><a href="https://i.stack.imgur.com/Svw3O.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Svw3O.png" alt="As in this image."></a></p>