XML Creator in Java

import java.io.File;

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.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;

 

 

/**

* @author lsuri <lakshay13@gmail.com>

*/

public class XmlCreator {

 

public static void main(String arg[]) {

 

try {

DocumentBuilderFactory documentBuilderFactory =          DocumentBuilderFactory.newInstance();

DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

 

//root Element

Document doc = documentBuilder.newDocument();

Element rootElement = doc.createElement(“CompanyInfo”);

doc.appendChild(rootElement);

 

//First Element

Element elem1 = doc.createElement(“Company1Info”);

elem1.setAttribute(“value”, “Microsoft”);

rootElement.appendChild(elem1);

 

//values under the elem1.

Element elementnode1= doc.createElement(“division1”);

elementnode1.appendChild(doc.createTextNode(“Development”));

elem1.appendChild(elementnode1);

 

Element elementnode2 = doc.createElement(“division2”);

elementnode2.appendChild(doc.createTextNode(“Testing”));

elem1.appendChild(elementnode2);

 

Element elementnode3 = doc.createElement(“division3”);

elementnode3.appendChild(doc.createTextNode(“Marketing”));

elem1.appendChild(elementnode3);

 

Element elementnode4 = doc.createElement(“division4”);

elementnode4.appendChild(doc.createTextNode(“Sales”));

elem1.appendChild(elementnode4);

 

//second element

Element elem2 = doc.createElement(“Company2Info”);

elem2.setAttribute(“value”, “Facebook”);

rootElement.appendChild(elem2);

 

Element elementnode1 = doc.createElement(“division1”);

elementnode1.appendChild(doc.createTextNode(“Research”));

elem2.appendChild(elementnode1);

 

Element elementnode2 = doc.createElement(“division2”);

elementnode2.appendChild(doc.createTextNode(“AppsTeam”));

elem2.appendChild(elementnode2);

 

Element elementnode3 = doc.createElement(“division3”);

elementnode3.appendChild(doc.createTextNode(“IntegrationTeam”));

elem2.appendChild(elementnode3);

 

Element elementnode4 = doc.createElement(“division4”);

elementnode4.appendChild(doc.createTextNode(“QATeam”));

elem2.appendChild(elementnode4);

 

 

//Third Element

Element elem3 = doc.createElement(“Company3Info”);

elem3.setAttribute(“value”, “Google”);

rootElement.appendChild(elem3);

 

//values of the elem.

Element elementnode1= doc.createElement(“division1”);

elementnode1.appendChild(doc.createTextNode(“BusinessDevTeam”));

elem3.appendChild(elementnode1);

 

Element elementnode2 = doc.createElement(“division2”);

elementnode2.appendChild(doc.createTextNode(“PreSalesTeam”));

elem3.appendChild(elementnode2);

 

// write the content into xml file

TransformerFactory transformerFactory = TransformerFactory.newInstance();

Transformer transformer = transformerFactory.newTransformer();

DOMSource source = new DOMSource(doc);

StreamResult result = new StreamResult(new FileOutputStream(“/src/main/resources/file1.xml”));

transformer.transform(source, result);

System.out.println(“File has been saved!”);

} catch (ParserConfigurationException pce) {

pce.printStackTrace();

} catch (TransformerException tfe) {

tfe.printStackTrace();

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s