Simple Java HTML syntax highlighter for XML code
This simple code snippet will generate better looking html for <pre>
elements with XML snippets in them. It may be dirty, but it's simple and effective.
public final String highlight(String source)
{
source = source.replaceAll("<([^>/]*)/>", "<~blue~$1~/~/>");
source = source.replaceAll("<([^>]*)>", "<~blue~$1~/~>");
source = source.replaceAll("([\\w]+)=\"([^\"]*)\"", "~red~$1~/~~black~=\"~/~~green~$2~/~~black~\"~/~");
source = source.replaceAll("~([a-z]+)~", "<span style=\"color: $1;\">");
source = source.replace("~/~", "</span>");
return source;
}
Written by Wouter Lievens
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Xml
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#