?
Solved

Modifying HTML files with Java

Posted on 2008-06-11
3
Medium Priority
?
225 Views
Last Modified: 2013-12-29
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?
0
Comment
Question by:jsonburke
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
mbodewes earned 2000 total points
ID: 21764399
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.
0
 
LVL 9

Expert Comment

by:mbodewes
ID: 21764412
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.
0
 

Author Closing Comment

by:jsonburke
ID: 31466334
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!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question