Creating XML Tree

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

Creating XML Tree

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.


Xmltree.java

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

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

public class Xmltree {

  public static void main(String[] argsthrows Exception {
  DocumentBuilderFactory builderFactory = 
   DocumentBuilderFactory.newInstance
();
   DocumentBuilder docBuilder = 
   builderFactory.newDocumentBuilder
();
  //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.
  doc.appendChild(root);

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

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

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

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

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

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

  Element chilE = doc.createElement("Id");
  chilE.setAttribute("name""Girish");
  root.appendChild(chilE);

  // adding a text element to the child
  Text text12 = doc.createTextNode("status");
  chilE.appendChild(text12);
 
 //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)));
  bw.write(xmlString);
  bw.flush();
  bw.close();
  // 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"?>
<Company>
<Location>
<Companyname>Roseindia .Net</Companyname>
<!--Employee in roseindia-->
<Employee>Girish Tewari</Employee>
</Location>
<Id name="Girish">status</Id>
</Company>

DownLoad SourceCode