Create XML File using DOM Parser of JAXP and Transformation APIs

We have already studied about :

  1. What is DOM and its structure
  2. JAXP for DOM Parsers

We will create a XML file using DOM parser and Transformation APIs in this article.

package com.G2.DOM;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
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 CreateXMLFile {

	public static void main(String[] args)  {

		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document doc = builder.newDocument();

			Element ele = doc.createElement("Movies");

			Element childElement = doc.createElement("Movie");

			childElement.setAttribute("Type", "BollyWood");
			childElement.setAttribute("Name", "Lagaan");
			childElement.setAttribute("Actor", "Aamir Khan");

			//You can also use setTextContent() method to write between nodes

			ele.appendChild(childElement);

			doc.appendChild(ele);

			//Save the Created XML on Local Disc using Transformation APIs as Discussed
			TransformerFactory tFactory = TransformerFactory.newInstance();
			Transformer transformer = tFactory.newTransformer();

                        //below 2 line are only for pretty look rendering of XML
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

			Source s = new DOMSource(doc);
			Result res = new StreamResult( new FileOutputStream("ShivaSoft.xml"));
			transformer.transform(s, res);
			System.out.println("XML File Created Succesfully");

		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
		catch(TransformerConfigurationException e)
		{
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		}
	}

}

Note : The XML document can also be saved on local disc by serializing xml document first and then saving. But the serialization implementation differs for different APIS. So the best practice is using implementation independent method of saving xml document. To read more refer this URL: http://java.sun.com/developer/technicalArticles/xml/JavaTechandXML_part3/

Posted

in

by


Related Posts

Comments

3 responses to “Create XML File using DOM Parser of JAXP and Transformation APIs”

  1. […] Traverse XML file using DOM Parser of JAXP Posted by Jitendra Zaa on March 22nd, 2011 Check this post to get the idea about how to create XML document using DOM Parser and XSLT API. […]

  2. […] Create XML file using DOM […]

  3. […] Parser in JAVA Posted by Jitendra Zaa on March 22nd, 2011 We have already discussed about the DOM Parser of JAXP. This article will focus on Reading XML file using SAX […]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading