Home Java Example Java XmlParsing Java Generate XML Example
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Java Generate XML Example

Advertisement
In this tutorial you will learn that how to create a xml file in Java

Java Generate XML Example

To Create a XML in Java using Dom follow the Steps

1. Get the DocumentBuilderFactory instance

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

2. Get DocumentBuilder instance using DocumentBuilderFactory
DocumentBuilder documentBuilder = dbFactory.newDocumentBuilder();

3. Create a document object from documentBuilder
// Create a new document
document = documentBuilder.newDocument();

4. Create a root elements and add it into the document
// Creating a root element
Element rootElement = document.createElement("RootElement");
// Add it to the Document
document.appendChild(rootElement );

5. Create the child element
// Creating student elements 
Element ChildElement = document.createElement("Child");
		
//adding attribute to student element
ChildElement .setAttribute("property", "ChildProperty");

6. Add the child element to the root element
rootElement.appendChild(ChildElement);

7. Create the output formate
// Creating XML output format
OutputFormat xmlOutputFormat = new OutputFormat(document);
xmlOutputFormat.setIndenting(true);

8. Create a Serialiser
XMLSerializer consoleSerializer = new XMLSerializer(System.out,	xmlOutputFormat);
XMLSerializer fileSerializer = new XMLSerializer(new FileOutputStream(new File("C:\\college.xml")),	xmlOutputFormat);

9. Serialise the document fileSerializer.serialize(document); An example for creating the XML in Java is given below
package net.roseindia;

import java.io.File;
import java.io.FileOutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

public class JavaCreateXml {
	Document document;

	public JavaCreateXml() {
	}

	public void createXmlDocument() {

		// Get The Document builder factory instance
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
				.newInstance();
		try {
			// Create the documentBuilder instance from documentBuilder
			DocumentBuilder documentBuilder = documentBuilderFactory
					.newDocumentBuilder();

			// Create a new document
			document = documentBuilder.newDocument();

			// Creating a root element
			Element collegeElement = document.createElement("College");

			// Add it to the Document
			document.appendChild(collegeElement);

			// Getting all the student elements
			Element studentElement = createStudentElement();
			collegeElement.appendChild(studentElement);

			// Getting all the faculity elements
			Element faculity = createFaculityElement();
			collegeElement.appendChild(faculity);

			// Creating XML output format
			OutputFormat xmlOutputFormat = new OutputFormat(document);
			xmlOutputFormat.setIndenting(true);

			// Creating XML output format for Console
			XMLSerializer consoleSerializer = new XMLSerializer(System.out,
					xmlOutputFormat);
			// Creating XML output format for xml file
			XMLSerializer fileSerializer = new XMLSerializer(
					new FileOutputStream(new File("C:\\college.xml")),
					xmlOutputFormat);

			// Serializing the format
			consoleSerializer.serialize(document);
			fileSerializer.serialize(document);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public Element createStudentElement() {
		// Creating student elements
		Element studentElement = document.createElement("Student");

		// adding attribute to student element
		studentElement.setAttribute("type", "regular");

		// Adding name elements to student
		Element studentNameElement = document.createElement("Name");
		Text nameText = document.createTextNode("Ram");
		studentNameElement.appendChild(nameText);
		studentElement.appendChild(studentNameElement);

		// Adding Course elements to student
		Element studentCourseElement = document.createElement("Course");
		Text courseText = document.createTextNode("B.Tech");
		studentCourseElement.appendChild(courseText);
		studentElement.appendChild(studentCourseElement);

		// Adding Address elements to student
		Element studentAddressElement = document.createElement("Address");
		Text addressText = document.createTextNode("New Delhi");
		studentAddressElement.appendChild(addressText);
		studentElement.appendChild(studentAddressElement);

		return studentElement;
	}

	public Element createFaculityElement() {
		// Creating Faculity elements
		Element faculityElement = document.createElement("Faculity");
		faculityElement.setAttribute("type", "regular");

		Element faculityNameElement = document.createElement("Name");
		Text nameText = document.createTextNode("Toni Dicosta");
		faculityNameElement.appendChild(nameText);
		faculityElement.appendChild(faculityNameElement);

		Element faculitySubjectElement = document.createElement("Subject");
		Text subjectText = document.createTextNode("Java");
		faculitySubjectElement.appendChild(subjectText);
		faculityElement.appendChild(faculitySubjectElement);

		return faculityElement;
	}

	public static void main(String[] args) {
		new JavaCreateXml().createXmlDocument();
	}
}


When you run this application it will display message as shown below:


<?xml version="1.0" encoding="UTF-8"?>
<College>
<Student type="regular">
<Name>Ram</Name>
<Course>B.Tech</Course>
<Address>New Delhi</Address>
</Student>
<Faculity type="regular">
<Name>Toni Dicosta</Name>
<Subject>Java</Subject>
</Faculity>
</College>

Download Select Source Code

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 9, 2011

Ask Questions?    Discuss: Java Generate XML Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com