Solved

Modifying HTML files with Java

Posted on 2008-06-11
3
217 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 500 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now