How to create XML file using DOM Parser

In this tutorials we going to see how to create the XML file using Java DOM parser. In previous example I explained about how to read XML file using DOM parser.

CreateXMLFile.java

package com.ehowtonow.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) {
  
  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) {
   e.printStackTrace();
  } catch (TransformerConfigurationException e) {
   e.printStackTrace();
  } catch (TransformerException e) {
   e.printStackTrace();
  }

 }

}

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

DOM parser created student.xml

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

Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. Java DOM XML Parser
  2. How to read simple XML file using DOM Parser
  3. How to Read XML File using DOM parser
  4. How to Modify XML file using DOM Parser
  5. How to create XML file using DOM Parser
  6. How to Count number of Element in XML

Be the first to comment

Leave a Reply