Generate XML Document

Generate XML Document


Posted in : Java Posted on : April 20, 2012 at 7:14 PM Comments : [ 0 ]

In this tutorial you will learn how to generate an XML document in java

Creating An XML Document Using Java

To create a XML document in java first create a Document instance from DocumentBuilderFactory as,

	DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
	DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
	Document document = documentBuilder.newDocument();

Now create a Root Node of XML as

	Element root = document.createElement("Student");
	document.appendChild(root);

Append chid to the node

	Element child1 = document.createElement("Name");
	child1.appendChild(document.createTextNode("Joe"));
	root.appendChild(child1);

Then transform the XML document as

	TransformerFactory transformerFactory = TransformerFactory.newInstance();
	Transformer transformer = transformerFactory.newTransformer();
	DOMSource source = new DOMSource(document);

And finally save the XML file to the database

	StreamResult result = new StreamResult("G:\\xmlfiles\\student.xml");
	transformer.transform(source, result);

The complete example is given below

GenerateXML.java

package dom;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class GenerateXML {
	public static void main(String[] args) throws Exception {

		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
		Document document = documentBuilder.newDocument();
		
		/* Creating Root Element */
		Element root = document.createElement("Student");
		document.appendChild(root);
		
		/* Creating First Child */
		Element child1 = document.createElement("Name");
		child1.appendChild(document.createTextNode("Joe"));
		root.appendChild(child1);
		
		/* Creating Second Child */
		Element child2 = document.createElement("Course");
		child2.appendChild(document.createTextNode("MBA"));
		root.appendChild(child2);

		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		DOMSource source = new DOMSource(document);
		
		/* Creating an XML document student.xml */
		StreamResult result = new StreamResult("G:\\xmlfiles\\student.xml");
		transformer.transform(source, result);
		
		/* Printing XML file on the console */
		StreamResult consoleResult = new StreamResult(System.out);		
		transformer.transform(source, consoleResult);
	}
}


When you run this application it will display message as shown below:


<?xml version="1.0" encoding="UTF-8" standalone="no"?><Student><Name>Joe</Name><Course>MBA</Course></Student>

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics