Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-07-2008, 11:55 PM
Member
 
Join Date: Oct 2008
Posts: 19
newbieal is on a distinguished road
use of javax.swing.text.html
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?
Code:
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); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-08-2008, 01:43 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
Quote:
html document is actually updated
Where are the output statements that write the results to the file?
There are several code examples on this site that write to files.
Use the Search

Add the following to your code to see what is output:
// Now output the doc
BufferedWriter bw = new BufferedWriter(new FileWriter(outFN));
kit.write(bw, doc, 0, doc.getLength());
bw.close();

I tried this on an HTML file and found that the output was missing some text that was in the input file. >>> Don't recommend this as a way to EDIT an HTML file. It looses text!!

Last edited by Norm : 10-08-2008 at 09:05 PM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-09-2008, 08:17 PM
Member
 
Join Date: Oct 2008
Posts: 19
newbieal is on a distinguished road
thanks
I appreciate the feedback. In the meanwhile I read up on java dom a little bit and have decided to switch to it to update html docs. However, the code is not working yet. Is this something you could help me with, or should I create a new post for it?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-09-2008, 08:45 PM
CJSLMAN's Avatar
Senior Member
 
Join Date: Oct 2008
Location: Guadalajara, Mexico
Posts: 201
CJSLMAN is on a distinguished road
I vote new post ...
I would suggest opening a new post, since the topic is different.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
java.lang.NoClassDefFoundError: javax/swing/GroupLayout$Group scotter59 NetBeans 6 07-10-2008 09:28 PM
Unsupported Content-Type: text/html Supported ones are: [text/xml] luislopezco Advanced Java 0 05-26-2008 06:26 PM
HTML on Swing Components Java Tip Java Tips 0 11-27-2007 11:51 AM
Error: javax.swing.AbstractButton cannot be applied to... barney AWT / Swing 1 08-01-2007 08:10 AM
map javax.swing.text.Element to javax.swing.text.View elizabeth New To Java 1 07-30-2007 09:02 PM


All times are GMT +3. The time now is 11:08 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org