tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Tools and Libs > Jsoup > Append Element

Append Element 

Jsoup is an open source java library for parsing and manipulating HTML with ease. Get the latest binaries from http://jsoup.org/. This requires the library jsoup-1.6.1.jar to be in classpath. The following example shows appending a tag element to another element.

File Name  :  
com/bethecoder/tutorials/jsoup/tests/AppendElementTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.jsoup.tests;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class AppendElementTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    String html =   "<html>" +
              "<head><script src='jquery.js'></script></head>" 
              "<body>BE THE CODER</body>" +
            "</html>";
      
    Document doc = Jsoup.parse(html);
    System.out.println(doc.html());
    
    System.out.println();
    
    //Append new script tag to head
    doc.head().appendElement("script").attr("src""ToolTip.js");
    
    //Append new SPAN to body
    doc.body().appendElement("span").attr("id""data").text("BTC");
    
    System.out.println(doc.html());
  }

}
   

It gives the following output,
File Name  :  OUTPUT



 
  


  
bl  br