2

I have to convert a .log file into a nice and pretty HTML file with tables. Right now I just want to get the HTML header down. My current method is to println to file every single line of the HTML file. for example

p.println("<html>");

p.println("<script>");

etc. there has to be a simpler way right?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Lehan
  • 21
  • 1
  • 2
  • 3
  • another thing, i have a whole chunk of HTML code that HAS to be in there. Is there a function that prints the entire chunk to file, without having to manually type in each line? – Lehan Jun 23 '11 at 19:14

4 Answers4

2

How about using a JSP scriplet and JSTL?, you could create some custom object which holds all the important information and display it formatted using the Expression Language.

Tristian
  • 3,406
  • 6
  • 29
  • 47
2

Printing raw HTML text as strings is probably the "easiest" (most straightforward) way to do what you're asking but it has its drawbacks (e.g. properly escaping the content text).

You could use the DOM (e.g. Document et al) interface provided by Java but that would hardly be "easy". Perhaps there are "DOM builder" type tools/libraries for Java that would simplify this task for you; I suggest looking at dom4j.

maerics
  • 151,642
  • 46
  • 269
  • 291
  • I was actually given a ruby script that does the same thing, albeit the formatting is a little different. In there was a method that performs the proper escapes, so the straighforward method might be the quickest. Thanks – Lehan Jun 23 '11 at 19:12
1

Look at this Java HTML Generator library (easy to use). It should make generating the actual HTML muuuch clearer. There are complications when creating HTML with Java Strings (what happens if you want to change something like a rowspan?) that can be avoided with this library. Especially when dealing with tables.

0

There are many templating engines available. Have a look at https://stackoverflow.com/questions/174204/suggestions-for-a-java-based-templating-engine This way you can define a template in a txt file and have the java code fill in the variables.

Community
  • 1
  • 1
James Scriven
  • 7,784
  • 1
  • 32
  • 36