Explanation:

This sample example will construct a XML / DOM / Document Object in java.

We will create new instance of DocumentBuilderFactory using DocumentBuilderFactory.newInstance() method. The newDocumentBuilder will create new instance of DocumentBuilder with configured parameters. The newDocument() method will create new instance of Document Object to Build a DOM Tree.Element is an interface from org.w3c.dom package which extends from Node interface. Elements represents an element from an XML document.These Elements may have attributes, Nodes. This interface provides method which can help us build, read and edit a complex Dom Tree.

This is the XML which we wish to generate dynamically using Java Document Object Model (DOM)

 

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<DevelopProjects rootAttr="true">
	<FirstChild flag="1">
		This is the First Child in DevelopProjects.com
		<FirstChildNode flag="1.1">
			This is the Child Element of FirstChild
			<FirstSubChildNode flag="1.1.1">This is the Sub Child
				Element of FirstChild</FirstSubChildNode>
		</FirstChildNode>
	</FirstChild>
	<SecondChild flag="2">This is the Second Child in
		DevelopProjects.com</SecondChild>
	<ThirdChild flag="3" />
	<FourthChild flag="4" />
	<FifthChild flag="5" />
	<SixthChild flag="6" />
	<SeventhChild flag="7" />
	<EigthChild flag="8" />
	<NinethChild flag="9" />
	<TenthElement>This is how I'm 10 Now</TenthElement>
</DevelopProjects>

Sample Java Program to Create XML Object

 

package com.developprojects.java;

import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
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.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class CreateXML {

/**
 * @param args
 */
 public static void main(String[] args) {
 // TODO Auto-generated method stub
 System.out.println(generateXML());
 }
 public static String generateXML(){
 System.out.println("generateXML() Entry");
 Document doc=null;
 StringWriter writer = new StringWriter();

 try {
 final String FLAG = "flag";
 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
 DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
 doc = docBuilder.newDocument();
 Element rootElement = doc.createElement("DevelopProjects");
 doc.appendChild(rootElement);
 Attr attr = doc.createAttribute("rootAttr");
 attr.setValue("true");
 rootElement.setAttributeNode(attr);

 Element firstChildNode = doc.createElement("FirstChildNode");
 attr = doc.createAttribute(FLAG);
 attr.setValue("1.1");
 firstChildNode.setAttributeNode(attr);
 firstChildNode.appendChild(doc.createTextNode("This is the Child Element of FirstChild"));

 Element firstSubChildNode = doc.createElement("FirstSubChildNode");
 attr = doc.createAttribute(FLAG);
 attr.setValue("1.1.1");
 firstSubChildNode.setAttributeNode(attr);
 firstSubChildNode.appendChild(doc.createTextNode("This is the Sub Child Element of FirstChild"));

 Element firstChildElement = doc.createElement("FirstChild");
 attr = doc.createAttribute(FLAG);
 attr.setValue("1");
 firstChildElement.setAttributeNode(attr);
 firstChildElement.appendChild(doc.createTextNode("This is the First Child in DevelopProjects.com"));
 firstChildElement.appendChild(firstChildNode);
 firstChildNode.appendChild(firstSubChildNode);
 rootElement.appendChild(firstChildElement);

 Element secondChildElement = doc.createElement("SecondChild");
 attr = doc.createAttribute(FLAG);
 attr.setValue("2");
 secondChildElement.setAttributeNode(attr);
 secondChildElement.appendChild(doc.createTextNode("This is the Second Child in DevelopProjects.com"));
 rootElement.appendChild(secondChildElement);

 Element thirdChildElement = doc.createElement("ThirdChild");
 attr = doc.createAttribute(FLAG);
 attr.setValue("3");
 thirdChildElement.setAttributeNode(attr);
 rootElement.appendChild(thirdChildElement);

 Element fourthChildElement = doc.createElement("FourthChild");
 attr = doc.createAttribute(FLAG);
 attr.setValue("4");
 fourthChildElement.setAttributeNode(attr);
 rootElement.appendChild(fourthChildElement);

 Element fifthChildElement = doc.createElement("FifthChild");
 attr = doc.createAttribute(FLAG);
 attr.setValue("5");
 fifthChildElement.setAttributeNode(attr);
 rootElement.appendChild(fifthChildElement);

 Element sixthChildElement = doc.createElement("SixthChild");
 attr = doc.createAttribute(FLAG);
 attr.setValue("6");
 sixthChildElement.setAttributeNode(attr);
 rootElement.appendChild(sixthChildElement);

 Element seventhChildElement = doc.createElement("SeventhChild");
 attr = doc.createAttribute(FLAG);
 attr.setValue("7");
 seventhChildElement.setAttributeNode(attr);
 rootElement.appendChild(seventhChildElement);

 Element EightChildElement = doc.createElement("EigthChild");
 attr = doc.createAttribute(FLAG);
 attr.setValue("8");
 EightChildElement.setAttributeNode(attr);
 rootElement.appendChild(EightChildElement);

 Element NinethChildElement = doc.createElement("NinethChild");
 attr = doc.createAttribute(FLAG);
 attr.setValue("9");
 NinethChildElement.setAttributeNode(attr);
 rootElement.appendChild(NinethChildElement);

 Element tenthElement = doc.createElement("TenthElement");
 tenthElement.appendChild(doc.createTextNode("This is how I'm 10 Now"));
 rootElement.appendChild(tenthElement);

 TransformerFactory transformerFactory = TransformerFactory.newInstance();
 Transformer transformer = transformerFactory.newTransformer();
 DOMSource source = new DOMSource(doc);
 StreamResult result = new StreamResult(writer);
 transformer.transform(source, result);

 } catch (ParserConfigurationException pce) {
 System.out.println("Exception Occured While parsing the XML::"+pce);
 }catch (TransformerException tfe) {
 System.out.println("Exception Occured While Transforming the XML::"+tfe);
 }
 System.out.println("generateXML() Entry");
 return writer.toString();
 }

}


Swarup Reddy (13 Posts)

Founder of Developprojects.com. He is an Oracle certified expert java developer. He is proficient in end-to-end development right from requirement analysis to system study, designing, coding, testing, de- bugging, documentation and implementation. His areas of interests are Information Security, Programming, compiler designing and automation.