I've the following code. What I'm trying to accomplish is update text within the <head> tag of any given html document. Can someone please help and give me an idea what the code needs to look like so that the html document is actually updated?
import java.io.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import javax.swing.text.ElementIterator;
class testingWriteToHTML {
public static void main(String[] args) {
EditorKit kit = new HTMLEditorKit();
Document doc = kit.createDefaultDocument();
// The Document class does not yet
// handle charset's properly.
doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
String fn = null;
try {
// Create a reader on the HTML content.
fn = System.getProperty("user.dir") + System.getProperty("file.separator") + "testingHTML.html";
BufferedReader reader = new BufferedReader(new FileReader(fn));
// Parse the HTML.
kit.read(reader, doc, 0);
// Iterate through the elements
// of the HTML document.
ElementIterator it = new ElementIterator(doc);
javax.swing.text.Element elem;
while ((elem = it.next()) != null) {
// System.out.print( "Attributes name = " + elem.getAttributes());
if ( elem.getParentElement() != null)
{
// System.out.println( elem.getStartOffset() + " " + elem.getEndOffset() + " Element = " + elem.getName() + " Parent " + elem.getParentElement().getName());
if(elem.getName() == "head")
{
doc.insertString(0, "TESTING", null);
System.out.println( " Element = " + elem.getName() + " " + doc.getText( elem.getStartOffset(), elem.getEndOffset()-elem.getStartOffset()));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
System.exit(0);
}
}