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

    Default java DOM: append text to node

    I've got this code that currently overwrites whatever is in the <head> tag, but I want to append new text instead of overwriting it. How can this be done?

    Java Code:
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(false);
    dbf.setNamespaceAware(false);
    dbf.setExpandEntityReferences(false);
                            
    DocumentBuilder db = dbf.newDocumentBuilder ( );
                    
    Document doc = db.parse ( new File ( "testing.html" ) );
               
    String newinfo = "add this line";
               
    Element root = doc.getDocumentElement ( );
    System.out.println ( root.getNodeName ( ) );
    NodeList list = root.getChildNodes ( );
    for ( int i = 0; i < list.getLength ( ); i++ ) 
    {
       if ( list.item ( i ).getNodeName ( ).equals ( "head" ) ) 
    {
           list.item ( i ).setTextContent ( newinfo );
    }
    }
    XMLSerializer serializer = new XMLSerializer ( );
    serializer.setOutputCharStream ( new java.io.FileWriter ( "testing.html" ) );
    serializer.serialize ( doc );

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,606
    Rep Power
    23

    Default

    DO you have a small source to demonstrate your problem?
    Something that compiles and executes.
    append new text instead of overwriting it
    Can you give an example of this? IE a before and after

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

    Default

    ?xml version="1.0"?>
    <html xmlns="w3.org/1999/xhtml">
    <head>some text</head>
    <body>

    <p>more text</p>
    </body>
    </html>

Similar Threads

  1. Can we append more than 255 chars to <a href>??
    By freddieMaize in forum Advanced Java
    Replies: 22
    Last Post: 07-18-2008, 04:04 PM
  2. How to disabled a node.
    By smartsubroto in forum New To Java
    Replies: 32
    Last Post: 07-01-2008, 07:30 AM
  3. JLabel append?
    By Jononomous in forum New To Java
    Replies: 0
    Last Post: 04-07-2008, 07:41 PM
  4. Simple append question
    By Rageagainst20 in forum New To Java
    Replies: 0
    Last Post: 12-20-2007, 11:40 PM
  5. Replies: 1
    Last Post: 08-07-2007, 05:35 AM

Posting Permissions

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