?
Solved

Modifying HTML files with Java

Posted on 2008-06-11
3
Medium Priority
?
223 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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: …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

801 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