Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Creating XML Tree

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


JAXB Create XML File And Get Data From XML XML,XML Tutorials,Online XML Tutorial,XML Help Tutorials The Simple API for XML (SAX) APIs Part I XML,XML Tutorials,Online XML Tutorial,XML Help Tutorials XML Tutorial Ignoring Comments While Parsing an XML File Accessing XML file from Java Preventing Expansion of Entity References Getting next Tag in the XML File Replacing a Node with a New One Reading XML Data from a Stream Reading an XML document using JDOM Query XML with an XPath expression Normalize All of the Text in a Document Modifying Text by Replacement Insert a Processing Instruction and a Comment Node Getting text values from a NodeList Finding an ID Value and Print the Element Text Edit Text by Insertion and Replacement Duplicating a Subtree Deleting Two Attributes Delete the First Child of the Root Node Creating XML Tree Creating a DocumentFragment Subtree and Appending it to the Document Creates element node, attribute node, comment node, processing instruction and a CDATA section Creates a New DOM Parse Tree Replacing a Text Node with a New CDATA Section Node Create an Empty DOM Document Adding an Attribute in DOM Document Transforming XML with XSLT Transforming an XML File with XSL Transforming XML with SAXFilters Storing properties in XML file Testing EntityReferences in Xml Splitting One Text Node into Three Reading XML from a File Modifying Text by Cutting and Pasting Locating a Node by Using Siblings Locate a Node and Change Its Content Loading properties from a XML file Listing nodes used in a document Inserting a New Entry in a List Creating an InlineStreamFilter Ignoring Comments While Parsing an XML File Handling Errors While Parsing an XML File Emitting DOCTYPE Declaration while writing XML File Converting CDATA Nodes into Text Nodes Java DOM Tutorial Creating Blank 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