Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

Features

Technology
  Tech. Home
  GPS
  OSI
  WiMAX
  VoIP
  WiFi
  HSDPA
  LBS
  Vehicle Tracking
  SCADA
  Tech. What is?
Jobs At RoseIndia.net!
 
Join For Newsletter

Powered by groups.yahoo.com
Visit Group! Post Questions!

Cloning a XML Element

                         

In this section, you will learn to create a clone of a  element in the DOM tree. In general, the cloning means to create a duplicate. 

Description of a program:

The following program helps you in creating a clone of any element of the specified XML file. For creating a DOM object ,  you need the  DocumentBuilderFactoty and the DocumentBuilder objects. After parsing it displays a xml file on the console using the transform() method. At  run time the program asks for a  element name to clone . Here the element1.cloneNode(true) method creates a clone and element1.getParentNode().insertBefore(copyElement, element1.getNextSibling()) inserts the clone element at the specified position.

Here is the XML File: Employee-Detail2.xml

<?xml version = "1.0" ?>
<Employee-Detail>
<Employee>
<Emp_Id> E-001 </Emp_Id>
<Emp_Name> Vinod </Emp_Name>
<Emp_E-mail> Vinod1@yahoo.com </Emp_E-mail>
</Employee>
</Employee-Detail>

Here is the Java File: DOMCloneElements.java

import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import javax.xml.transform.*; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamResult;

public class DOMCloneElements {
  static public void main(String[] arg){
    try  {
      BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
      System.out.print("Enter XML file name: ");
      String xmlFile = bf.readLine();
      File file = new File(xmlFile);
      if (file.exists()){
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(xmlFile);
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer tformer = tFactory.newTransformer();
        Source source = new DOMSource(doc);
        Result result = new StreamResult(System.out);
        System.out.println(xmlFile + " file: ");
        tformer.transform(source, result);
        System.out.println();
        System.out.print("Enter the element to clone: ");
        String clone = bf.readLine();
        System.out.print("Enter data to add: ");
        String addElement = bf.readLine();
        ////////////////////////////////
        NodeList list = doc.getElementsByTagName
(clone);
        Element element1 = (Element)list.item(0);
        Element copyElement = (Elementelement1.cloneNode(true);
        element1.getParentNode().insertBefore(copyElement, 
                                  element1.getNextSibling
());
        element1.appendChild(doc.createTextNode(addElement));
        tformer.transform(source, result);
      }
      else{
        System.out.println("File not found!");
      }
    }
    catch (Exception e){
      e.getMessage();
    }
  }
}

Download this example.

 Output of this program:

C:\vinod\xml>javac DOMCloneElements.java

C:\vinod\xml>java DOMCloneElements
Enter XML file name: Employee-Detail2.xml
Employee-Detail2.xml file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><Employee-Detail>
<Employee>
<Emp_Id> E-001 </Emp_Id>
<Emp_Name> Vinod </Emp_Name>
<Emp_E-mail> Vinod1@yahoo.com </Emp_E-mail>
</Employee>
</Employee-Detail>
Enter the element to clone: Emp_Id
Enter data to add: E011
<?xml version="1.0" encoding="UTF-8" standalone="no"?><Employee-Detail>
<Employee>
<Emp_Id> E-001 E011</Emp_Id><Emp_Id> E-001 </Emp_Id>
<Emp_Name> Vinod </Emp_Name>
<Emp_E-mail> Vinod1@yahoo.com </Emp_E-mail>
</Employee>
</Employee-Detail>

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.