Monday 5 July 2021

Create XML Using DOM Parser in JAVA

The Document Object Model (DOM) is an official recommendation of the World Wide Web Consortium (W3C). DOM reads the entire document and will be useful in case of the size of the XML is small. Performance-wise this is slow compared to other parsers since it loads the entire document. We can Perform the Operations using the DOM API.Its Stays in Tree Structure.


We can construct the Following XML using the DOM Parser.



We need to understand the following to get started.

The Node interface is the primary datatype for the entire Document Object Model. It represents a single node in the document tree. 

The Element interface represents an element in an HTML or XML document. Elements may have attributes associated with them; since the Element interface inherits from Node, the generic Node interface attribute attributes may be used to retrieve the set of all attributes for an element. 

In order to use the dom parser include the following dependency in the maven.

 <dependency>

   <groupId>xml-apis</groupId>

   <artifactId>xml-apis</artifactId>

   <version>1.4.01</version>

</dependency>


In the case of the Spring boot project, this should be included as part of the starter package itself I guess.


package com.searchendeca.main;

import java.io.StringWriter;


import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.transform.OutputKeys;

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.Document;

import org.w3c.dom.Element;

import org.w3c.dom.Node;


public class DomParserMain {


private Element createStudentElement(Document doc, Element rootElement) {

rootElement.appendChild(createElements(doc, "FirstName", "Syed"));

rootElement.appendChild(createElements(doc, "LastName", "Ghouse"));

rootElement.appendChild(createElements(doc, "City", "Salem"));

return rootElement;

}


private Node createElements(Document doc, String name, String value) {

Element node = doc.createElement(name);

if (value != null && !value.isEmpty()) {

node.appendChild((doc.createTextNode(value)));

}

return node;

}


public static void main(String args[]) throws ParserConfigurationException {


DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder dBuilder;

String sampleXml;

DomParserMain domParser = new DomParserMain();

dBuilder = dbFactory.newDocumentBuilder();

Document doc = dBuilder.newDocument();

Element rootElement = doc.createElement("Student");

doc.appendChild(rootElement);

rootElement = domParser.createStudentElement(doc, rootElement);

TransformerFactory factory = TransformerFactory.newInstance();

Transformer transformer = null;

try {

transformer = factory.newTransformer();

StringWriter writer = new StringWriter();

try {

transformer.setOutputProperty(OutputKeys.INDENT, "yes");

transformer.transform(new DOMSource(doc), new StreamResult(writer));

sampleXml = writer.getBuffer().toString();

System.out.println(sampleXml);

} catch (TransformerException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} catch (TransformerConfigurationException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}


}



The above code creates the XML in the desired output.


The Whole Project can be found in the Github link here.


Happy Learning!!!!!

No comments:
Write comments