How to add new attribute to the given node in XML file using DOM Parser in Java?
Problem Description:
<Employees>
<Employee id="1">
<name>abc</name>
<age>23</age>
</Employee >
<Employee id="2">
<name>xyz</name>
<age>29</age>
</Employee>
<Employee id="3">
<name>lmn</name>
<age>34</age>
</Employee>
<name>abc</name>
<age>23</age>
</Employee >
<Employee id="2">
<name>xyz</name>
<age>29</age>
</Employee>
<Employee id="3">
<name>lmn</name>
<age>34</age>
</Employee>
</Employees>
Here each Employee node has only one attribute id. But now my requirement is to add another attribute say yyy="xxx" to Employee whose id is 2. Similarly user can add any attribute to any given Employee node.
File file = new File(XML_file_location);
Here each Employee node has only one attribute id. But now my requirement is to add another attribute say yyy="xxx" to Employee whose id is 2. Similarly user can add any attribute to any given Employee node.
Steps:
1) Parse XML file or Create an instance of Document interface present in org.w3c.dom.* package.
For Creating an instance of Document interface we need to use DocumentBuilderFactory, DocumentBuilder classes present in javax.xml.parsers.* package and File class present in java.io.* package.File file = new File(XML_file_location);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(file);
2) Enter node id, attribute name and attribute value. Here node means Employee.
3) Get all the child nodes Employee of root node Employees. It returns the instance of NodeList interface present in org.w3c.dom.* package.
NodeList list = document.getElementsByTagName("Employee");
4) Now one by one examine each child node Employee.
2) Enter node id, attribute name and attribute value. Here node means Employee.
Figure 1 |
3) Get all the child nodes Employee of root node Employees. It returns the instance of NodeList interface present in org.w3c.dom.* package.
NodeList list = document.getElementsByTagName("Employee");
4) Now one by one examine each child node Employee.
- From all the child nodes that we got in Step 3 take one child node. It returns instance of Node interface present in org.w3c.dom.* package.
- Check whether this node is Element node or not?
- If it is an Element node then typecast it and get its data (attributes and properties).
- Get its id as follows.
- Compare this id with id of given Employee.
if (String.valueOf(id).equals(element.getAttribute("id"))) {}
- If id found then add its attribute as follows.
element.setAttribute(attribute, value);
5) Create an instance of Transformer class present in javax.xml.transform.* package.
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)
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.
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);
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.
Project Structure:
Figure 2 |
Java Code:
employee.xml
pom.xml
Employee.java
AddEmployeeAttribute.java
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>
pom.xml
Employee.java
AddEmployeeAttribute.java
Output:
employee.xml
Download Project
Click here for more questions on DOM Parser.
Figure 3 |
employee.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <Employees> <Employee id="1" nationality="indian"> <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>Here you can see that attribute nationality="indian" has been added for Employee whose id is 1.
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
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.
How to add new attribute to the given node in XML file using DOM Parser in Java?
Reviewed by Rohit Agarwal
on
10/09/2017
Rating:
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.