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/

This entry was posted in JAVA and tagged , , , on by .

About Jitendra

Sr. Consultant at Cognizant Technology Solutions. Have more than 7 years of experience in technologies and frameworks like Salesforce, Node.Js, JAVA, PHP, JSP, Struts, Hibernate, C#, ASP.NET, SQL Server. Also many javaScript libraries like AngularJs, JQuery, JQuery Mobile, BootStrap. Connect : Linked In

Related posts