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.
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.
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:
JDK
Eclipse
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.
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.
How to create XML file in Java using DOM Parser?
Reviewed by Rohit Agarwal
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.
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.