Results 1 to 4 of 4
  1. #1
    newbieal is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default 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?
    Java 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);
      }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    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 07:05 PM.

  3. #3
    newbieal is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default 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?

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default I vote new post ...

    I would suggest opening a new post, since the topic is different.

Similar Threads

  1. Replies: 6
    Last Post: 07-10-2008, 07:28 PM
  2. Replies: 0
    Last Post: 05-26-2008, 04:26 PM
  3. HTML on Swing Components
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-27-2007, 09:51 AM
  4. Replies: 1
    Last Post: 08-01-2007, 06:10 AM
  5. map javax.swing.text.Element to javax.swing.text.View
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 07:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •