Sunday 15 August 2010

java - Displaying Difference of an XML file in HTML format -



java - Displaying Difference of an XML file in HTML format -

i working on code show differences in 2 files in html format .i doing in java . have done far . 1. reading file contents string arrays . 2. using lcs algorithm find longest sub sequence matrix mentioned here 3. utilize string builder create html head 4. using lcs matrix , append strings string buffer. 5. if there difference in 2 strings alter tr bgcolour show in different color.

this works fine when utilize normal text file .

code snippet :

sb.append("<tr bgcolor='#ff0000'>"); sb.append("<td>"); sb.append( x[i++]); sb.append("</td>"); sb.append("<td>"); sb.append( y[j++]); sb.append("</td>"); sb.append("</tr>");

but if diff between 2 xml files not able see contents.

if text normal , html formed :

<td>normaltext</td>

//rendered properly

if xml files contains

<hello>

tag html formed contains

<td><hello></td>

because of browser not able render properly.

how can resolve ? pointers helpful.

replace < &lt; , > &gt;

problem solved.

to more concrete in case, next :

sb.append("<tr bgcolor='#ff0000'>"); sb.append("<td>"); sb.append( x[i++].replaceall("<", "&lt;").replaceall(">", "&gt;")); sb.append("</td>"); sb.append("<td>"); sb.append( y[j++].replaceall("<", "&lt;").replaceall(">", "&gt;")); sb.append("</td>"); sb.append("</tr>");

according comment, improve :

sb.append("<tr bgcolor='#ff0000'>"); sb.append("<td>"); sb.append( x[i++].replaceall("&", "&amp;").replaceall("<", "&lt;").replaceall(">", "&gt;")); sb.append("</td>"); sb.append("<td>"); sb.append( y[j++].replaceall("&", "&amp;").replaceall("<", "&lt;").replaceall(">", "&gt;")); sb.append("</td>"); sb.append("</tr>");

to have <= , >= not replaced, working solution, little nasty :) :

string x = "<hello>&<=<blabbalal>"; system.out.println(x.replaceall("&", "&amp;").replaceall("<", "&lt;").replaceall(">", "&gt;").replaceall("&gt;=", ">=").replaceall("&lt;=", "<="));

has output :

&lt;hello&gt;&amp;<=&lt;blabbalal&gt;

java html xml

No comments:

Post a Comment