How to create XML file in Java


package com.test;

import java.io.File;
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 CreateXMLInJava {

public static void main(String argv[]) {
CreateXMLInJava createXml = new CreateXMLInJava();
createXml.createXML();
}

public void createXML() {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

// root elements
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement(“Company”);
doc.appendChild(rootElement);

// staff elements
Element staff = doc.createElement(“Dept”);
rootElement.appendChild(staff);

// set attribute to staff element
Attr attr = doc.createAttribute(“id”);
attr.setValue(“1”);
staff.setAttributeNode(attr);

// can be written as
// staff.setAttribute(“id”, “1”);

// firstname elements
Element firstname = doc.createElement(“Firstname”);
firstname.appendChild(doc.createTextNode(“mallik”));
staff.appendChild(firstname);

// lastname elements
Element lastname = doc.createElement(“Lastname”);
lastname.appendChild(doc.createTextNode(“Gunda”));
staff.appendChild(lastname);

// salary elements
Element salary = doc.createElement(“Salary”);
salary.appendChild(doc.createTextNode(“100000”));
staff.appendChild(salary);

// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(“D:\\test.xml”));

// Output to console for testing
// StreamResult result = new StreamResult(System.out);

transformer.transform(source, result);

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

} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
}
}
}

Advertisements
About

I born in Good cultural place called India. My family is traditional family. We fallow all Indian traditions.

Tagged with: , , , , ,
Posted in JAVA, XML
One comment on “How to create XML file in Java
  1. Excellent write-up. I definitely love this website. Thanks!

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

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

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 678 other followers

Calendar
September 2012
M T W T F S S
« Jul   Oct »
 12
3456789
10111213141516
17181920212223
24252627282930
Mallikarjun Gunda

This is Mallikarjun Gunda, I am working as a software professional. Blogging is my Passion. This blog contains the posts on Java related technologies and programming languages. I am happy to share my knowledge through the blogs.

Blog Stats
  • 280,989 hits
IndiBlogger
malliktalksjava.wordpress.c...
70/100