How to create XML file in Java using DOM Parser?



Steps :


1) Create an instance of Document interface present in org.w3c.dom.* package.

For Creating an instance of Document interface we need to use DocumentBuilderFactory and DocumentBuilder classes present in javax.xml.parsers.* package.

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();

Create XML file using DOM parser
Figure 1

2) Create an instance of Element interface present in org.w3c.dom.* package. This is the root element of XML document. So root element is Employees.

Element root = document.createElement("Employees");

3) Make root as a child element of Document.

document.appendChild(root);


4) Now create child elements of root element Employees.

  • Let say we want to store 4 Employees data in XML then one by one we have to create 4 elements as below. So child element is Employee.
          Element child= document.createElement("Employee");
  • Each Employee should have some unique id that we can set as an attribute.
          child.setAttribute("id", value);
  • Each Employee has some properties (name, age, gender etc). Values of these properties can be different for every Employee. So for each property we need to create element as below.
           Element property = document.createElement(propertyName);
  • We can set its value as below.
          property.setTextContent(value);
  • One by one make these properties as a child element of Employee element as below.
          child.appendChild(property);
  • Finally make Employee element as a child element of Employees.
          root.appendChild(child);

5) Create an instance of Transformer class present in javax.xml.transform.* package. 

For creating an instance of Transformer class we need to use TransformerFactory class present in javax.xml.transform.* package.

TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();


6) Set xml file indentation (Optional)

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

7) Create an instance of DOMSource class present in javax.xml.transform.dom.* package and pass instance of Document interface created in step 1.

DOMSource source = new DOMSource(document);

8) Create an instance of StreamResult class present in javax.xml.transform.stream.* package.

For creating an instance of StreamResult class first we need to create an instance of File class present in java.io.* package.

File file = new File(location_of_xml_file);
StreamResult result = new StreamResult(file);


9) Transform XML Source to a Result.

transformer.transform(source, result);



Software Required:

  1. JDK
  2. Eclipse
  3. Maven
Note : I suggest to use latest version of all the software. If you need any help regarding these software installation please comment below I will help you.

Project Structure:


Java XML DOM Parser Project Stucture Part1
Figure 2

Java Code:


pom.xml


Employee.java


WriteXML.java


Output:


Java XML DOM Parser output part1
Figure 3

employee.xml


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Employees>
<Employee id="1">
<name>Rohit</name>
<gender>male</gender>
<age>25</age>
<role>Programmer</role>
</Employee>
<Employee id="2">
<name>Bhavna</name>
<gender>female</gender>
<age>20</age>
<role>Student</role>
</Employee>
</Employees>

Download Project


Click here for more questions on DOM Parser.

References:

https://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/DocumentBuilderFactory.html
https://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/DocumentBuilder.html



Thank you friends, I hope you have clearly understood the solution of this problem. If you have any doubt, suggestion or query please feel free to comment below. You can also discuss this solution in our forum.

About Author:

I am simple guy with lot of ambitions. My main motive is to share whatever knowledge I have related to programming. With me you can easily learn how to solve any programming problem in Java.You can connect with me on social networking sites also.


Let's Get Connected: Linkedin | Facebook |

How to create XML file in Java using DOM Parser? How to create XML file  in Java using DOM Parser? Reviewed by Unknown on 9/28/2017 Rating: 5

No comments:

Please provide your valuable comments. If you have any suggestion please share with me I will work on it and if you have any question or doubt please ask, don't hesitate. I am your friend, i will clarify all your doubts.

Powered by Blogger.