Solved

Modifying HTML files with Java

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet doXXX methods 3 62
javap not working 8 58
Why doesn't this text field show up on my Applet frame? 2 19
java mysql insert application 14 28
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

839 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