Java DOM Example

Here is an example explains how to create XML file using DOM API.domapi is very simple and easy to learn.below program explains step by step how to add XML header portion,content and footer portion for your XML file.Example

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
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.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Text;

public class JavaDomExample {

private String OutputFile = null;
private Document document_ = null;

public JavaDomExample() {
File outFile = new File(
“C:\\Users\\Mohan\\Desktop\\Test\\OutputFile.xml”);
OutputFile = outFile.getAbsolutePath();
createDoc(OutputFile);
addHeader(document_);
addContent(document_);
WriteToFile(document_, OutputFile);
}

private void addContent(Document document) {

Element root = document.getDocumentElement();

Element mainContentontechildElement = document
.createElement(“ContentStartHere”);

Element FirstContentchildElement = document.createElement(“Content1”);
Text ele = document.createTextNode(“1”);
FirstContentchildElement.appendChild(ele);

Element SecContentchildElement = document.createElement(“Content2”);
Text vntext = document.createTextNode(“2”);
SecContentchildElement.appendChild(vntext);

mainContentontechildElement.appendChild(FirstContentchildElement);
mainContentontechildElement.appendChild(SecContentchildElement);

root.appendChild(mainContentontechildElement);

}

private void WriteToFile(Document document, String outputFile) {

TransformerFactory transformerFactory = null;
Transformer transformer = null;

Source source = null;
Result output = null;
transformerFactory = TransformerFactory.newInstance();

try {
transformer = transformerFactory.newTransformer();
} catch (TransformerConfigurationException e) {
System.out.println(e.getMessage());

}

transformer.setOutputProperty(OutputKeys.INDENT, “yes”);
transformer.setOutputProperty(OutputKeys.ENCODING, “UTF8”);
Node doc;
source = new DOMSource(document);
File filetemp = null;
try {
filetemp = new File(outputFile);
output = new StreamResult(filetemp);
transformer.transform(source, output);

} catch (TransformerException e) {
System.out.println(e.toString());
}

}

public void addHeader(Document doc) {

Element root = doc.getDocumentElement();

root.setAttributeNS(“http://www.w3.org/2000/xmlns/”, “xmlns”,
“http://t/namespace/”);

root.setAttributeNS(“http://www.w3.org/2000/xmlns/”, “xmlns:xsi”,
“http://www.w3.org/2001/XMLSchema-instance”);
root.setAttribute(“xsi:schemaLocation”,
“http://www.test.com/namespace xsdFile.xsd”);
Element mainchildElement = doc.createElement(“Header”);

Element FirstchildElement = doc.createElement(“value1”);
Text ele = doc.createTextNode(“1”);
FirstchildElement.appendChild(ele);

Element SecchildElement = doc.createElement(“value2”);
Text vntext = doc.createTextNode(“2”);
SecchildElement.appendChild(vntext);

mainchildElement.appendChild(FirstchildElement);
mainchildElement.appendChild(SecchildElement);

root.appendChild(mainchildElement);
}

private Document createDoc(String outFile) {

DocumentBuilderFactory factory = null;
DocumentBuilder builder = null;
DOMImplementation dImpl = null;

try {
factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
dImpl = builder.getDOMImplementation();

document_ = dImpl.createDocument(null, “rootElementHere”, null);

}

catch (ParserConfigurationException e) {
System.out.println(e.getMessage());
}

return document_;

}

public static void main(String[] args) {
new JavaDomExample();
}
}

About mohan

This is Mohan Jayapalan author of TechPages Blog.I am a part time blogger and Currently working for private Software Concern.

© Copyright 2010-2016 www.techpages.org · All Rights Reserved · Powered by WordPress ·