728x90 AdSpace

Saturday, 9 November 2013

Create XML File using DOM Parser


In this tutorials we going to see how to create the XML document using DOM parser
1. Create Project Called JavaXML.
2. Create package called com.javatutorialscorner.xml.dom under JavaXML.
3. Create Java class called CreateXMLFile.java under com.javatutorialscorner.xml.dom package.
CreateXMLFile.java
package com.javatutorialscorner.xml.dom;

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.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.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class CreateXMLFile {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

try {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.newDocument();

Element root = document.createElement("college");
document.appendChild(root);

Element student = document.createElement("student");
root.appendChild(student);

Attr attr = document.createAttribute("id");
attr.setValue("ECE-01");
student.setAttributeNode(attr);

Element firsrName = document.createElement("firstname");
firsrName.appendChild(document.createTextNode("Sathish"));
student.appendChild(firsrName);

Element lastName = document.createElement("lastname");
lastName.appendChild(document.createTextNode("J"));
student.appendChild(lastName);

TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
DOMSource domSource = new DOMSource(document);
StreamResult streamResult = new StreamResult(new File(
"C:\\jtc\\student.xml"));
transformer.transform(domSource, streamResult);

} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

Now you can run the program see the XML file created at specified location.

student.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<college>
<student id="ECE-01">
<firstname>Sathish</firstname>
<lastname>J</lastname>
</student>
</college>
Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Create XML File using DOM Parser Rating: 5 Reviewed By: Annamalai Thangaraj