Creating XML Tree

In this section, you will learn how to create an XMLTree in a DOM document.

Creating XML Tree


This Example shows you how to Create an XMLTree in a DOM document. JAXP (Java API for XML Processing) is an interface which provides parsing of xml documents. Here the Document BuilderFactory is used to create new DOM parsers. Here we use "javax.xml.transform" package that defines the generic APIs to process transformation instructions, also performs a transformation from source to result. There are some of the methods used in code given below for Creating an XML Tree:-

Element root = doc.createElement("Company"):- This method creates an Element Node. Here "Company" is the String that specifies the name of the element node.

doc.appendChild(root):- This method adds a node after the last child node of the specified node.

Text text = doc.createTextNode("Roseindia .Net"):- This method creates a Text Node. Here "Roseindia.Net" is the String that specifies the text for the  node.

Comment comment = doc.createComment("Employee in roseindia"):- This method creates a Comment Node. Here "Employee in roseindia" is the String that specifies the comment for the node.

TransformerFactory factory = TransformerFactory.newInstance():-TransformerFactory is a class that is used to create Transformer objects. A TransformerFactory  instance can be used to create Transformer and Templates objects.

transformer.transform(source, result):-This function organize data according to a style sheet as we are retrieving it from the File.

File file = new File("newxml.xml"):-Creates a new File.

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))):-BufferedWriter is used to write Text to the File.

 * @Program to create a DOM XML tree and then print the XML.
 * Author:-RoseIndia Team
 * Date:-10-Jun-2008

import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;

public class Xmltree {

  public static void main(String[] argsthrows Exception {
  DocumentBuilderFactory builderFactory = 
   DocumentBuilder docBuilder = 
  //creating a new instance of a DOM to build a DOM tree.
  Document doc = docBuilder.newDocument();
  new Xmltree().createXmlTree(doc);

  public void createXmlTree(Document docthrows Exception {
  //This method creates an element node
  Element root = doc.createElement("Company");
 //adding a node after the last child node of the specified node.

  Element child = doc.createElement("Location");

  Element child1 = doc.createElement("Companyname");

  Text text = doc.createTextNode("Roseindia .Net");

  Comment comment = doc.createComment("Employee in roseindia");

  Element element = doc.createElement("Employee");

  Text text1 = doc.createTextNode("Girish Tewari");

  Element chilE = doc.createElement("Id");

  // adding a text element to the child
  Text text12 = doc.createTextNode("status");
 //TransformerFactory instance is used to create Transformer objects. 
  TransformerFactory factory = TransformerFactory.newInstance();
  Transformer transformer = factory.newTransformer();
  transformer.setOutputProperty(OutputKeys.INDENT, "yes");

  // create string from xml tree
  StringWriter sw = new StringWriter();
  StreamResult result = new StreamResult(sw);
  DOMSource source = new DOMSource(doc);
  transformer.transform(source, result);
  String xmlString = sw.toString();

  File file = new File("newxml.xml");
  BufferedWriter bw = new BufferedWriter
new OutputStreamWriter(new FileOutputStream(file)));
  // print xml
  System.out.println("Xml file created is :\n" 
  + xmlString

Output of the program

Xml file created is :
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Companyname>Roseindia .Net</Companyname>
<!--Employee in roseindia-->
<Employee>Girish Tewari</Employee>
<Id name="Girish">status</Id>

DownLoad SourceCode


Share on Google+Share on Google+

Creating XML Tree

Posted on: August 24, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Creating XML Tree  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
August 27, 2011

very helpful code...neat and clean..keep up the good word roseindia