In this tutorial, I will guide you how to edit an XML file using the DOM through an example is to edit the XML file has the following content:
1 2 3 4 5 6 7 8 |
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <students> <student n0='1'> <name>John</name> <code>12345</code> <age>19</age> </student> </students> |
The information will be edited
I will edit some things as follows:
- Correct student number (attribute n0) is 2.
- Name of student is Khanh.
- Add email tag with value (huongdanjava.com@gmail.com)
- Remove the age tag.
After editing, my XML file will look like this:
1 2 3 4 5 6 7 8 |
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <students> <student n0='2'> <name>Khanh</name> <code>12345</code> <email>huongdanjava.com@gmail.com</email> </student> </students> |
Edit
Similar to the tutorial on creating new XML file, to edit an XML file we also use DocumentBuilder to load the XML file and make the edit, then use the Transformer object to save the editing.
First, we will use the DocumentBuider object to create the Document object and load the XML file:
1 2 3 4 5 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder buider = factory.newDocumentBuilder(); File f = new File("students.xml"); Document doc = buider.parse(f); |
All the information we need to edit is in the <student> tag, so we have to get the information of this tag first:
1 |
Element student = (Element) doc.getElementsByTagName("student").item(0); |
Now we will update the attribute n0:
1 2 3 |
NamedNodeMap attr = student.getAttributes(); Node nodeAttr = attr.getNamedItem("n0"); nodeAttr.setTextContent("2"); |
To update the <name> tag information, we have to get this tag first from the <student> tag:
1 |
Element name = (Element) student.getElementsByTagName("name").item(0); |
Then, we will assign value to this tag as follows:
1 |
name.setTextContent("Khanh"); |
Next, we will add the <email> tag in the <student> tag:
1 2 3 |
Element email = doc.createElement("email"); email.appendChild(doc.createTextNode("huongdanjava.com@gmail.com")); student.appendChild(email); |
And remove the <age> tag in the <student> tag:
1 2 |
Element age = (Element) student.getElementsByTagName("age").item(0); student.removeChild(age); |
OK, now that we have finished editing, the rest is to save the edited content to disk only:
1 2 3 4 5 6 |
TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(f); transformer.transform(source, result); |
The code will look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
package com.huongdanjava; import java.io.File; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; 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.NamedNodeMap; import org.w3c.dom.Node; import org.xml.sax.SAXException; public class DomExample { public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, TransformerException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder buider = factory.newDocumentBuilder(); File f = new File("E://students.xml"); Document doc = buider.parse(f); Element student = (Element) doc.getElementsByTagName("student").item(0); // Update n0 attribute NamedNodeMap attr = student.getAttributes(); Node nodeAttr = attr.getNamedItem("n0"); nodeAttr.setTextContent("2"); // Update <name> tag Element name = (Element) student.getElementsByTagName("name").item(0); name.setTextContent("Khanh"); // Add <email> tag Element email = doc.createElement("email"); email.appendChild(doc.createTextNode("huongdanjava.com@gmail.com")); student.appendChild(email); // Delete <age> tag Element age = (Element) student.getElementsByTagName("age").item(0); student.removeChild(age); // Write to disk TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(f); transformer.transform(source, result); } } |
Result:
Jonh
Thank you. Beautiful example.
Khanh Nguyen
Thanks