Modifying HTML files with Java

I need to modify some parts of an html file used as a template for generating various other html files. For example: changing the <title></title> of a file. What is the most effective way to do this in Java?
Who is Participating?
mbodewesConnect With a Mentor Commented:
If you've generated this file yourself, you could use an XML parser, change the content of the title element and create well defined xhtml in return. This would be the very standard compliant, clean option. And one that is rather slow and very strict regarding the input. It also may remove whitespace etc. In memory XML documents normally need to go through a "transformer" to be written back to disk.

OTOH, a buffered reader (just reading the first 30 lines) together with a regular expression find/replace would do the trick as well, and chances are that it has better performance. Use a file writer to write the data back after the change the Strings.

In both cases you must be sure that the input is formatted in a compatible way, because if the HTML is too hairy, there is going to be problems whatever method you choose.
Ah, remove the "just reading the first 30 lines" from that solution. And I've even read back my answer, must get some sleep now.
jsonburkeAuthor Commented:
This worked perfectly. it seemed there were many possible directions to take with this, I agree that using a buffered reader with regular expressions is the most efficient way for this context. Thank you!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.