In the previous tutorial, we learned about how to read an XML file using the DOM in Java. In this tutorial, we will learn how to create new XML file using DOM!
The steps for creating a new XML file using the DOM include:
- Use the DocumentBuilder object to define the contents of the XML file
- Use Transformer object to write XML file to disk.
In this tutorial, we will go into the detail example to create XML file with 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> |
Definition of XML file content
We will first create a new Document object from the DocumentBuilder object.
1 2 3 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder buider = factory.newDocumentBuilder(); Document doc = buider.newDocument(); |
In the XML file above, the <students> tag is the outermost tag, root element. So we’ll add this tag first.
1 2 |
Element students = doc.createElement("students"); doc.appendChild(students); |
Now we will add the <student> tag inside the <students> tag:
1 2 |
Element student = doc.createElement("student"); students.appendChild(student); |
To add the n0=”1″ attribute to the <student> tag, we will use the Attr object:
1 2 3 |
Attr attr = doc.createAttribute("n0"); attr.setValue("1"); student.setAttributeNode(attr); |
Inside the <student> tag, we have more <name>, <code> and <age> tags, so our remaining task is to create 3 Elements for those 3 tags and add them to the <student> tag.
Name tag:
1 2 3 |
Element name = doc.createElement("name"); name.appendChild(doc.createTextNode("John")); student.appendChild(name); |
Code tag:
1 2 3 |
Element code = doc.createElement("code"); code.appendChild(doc.createTextNode("12345")); student.appendChild(code); |
Age tag:
1 2 3 |
Element age = doc.createElement("age"); age.appendChild(doc.createTextNode("19")); student.appendChild(age); |
Burn the XML file to disk
By using the Transformer object, we will write the contents of the file to the drive where we specify it.
First we need Transformer object before:
1 2 |
TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); |
In the Transformer object, it has a method called transform() with two parameters: the Source interface (created from the Document object containing the contents of the XML file) and the Interface Result (which contains information about the file XML after writing to the drive) to write XML file to the drive.
We will create the DOMSource object (this object implements the Source interface) to contain the contents of the file:
1 |
DOMSource source = new DOMSource(doc); |
And the StreamResult object (the implementation of the Result Interface) contains the information of the XML file:
1 2 |
File f = new File("D://students.xml"); StreamResult result = new StreamResult(f); |
Now we can call the Transformer object’s transform() method to write the contents of the file to disk:
1 |
transformer.transform(source, result); |
Full code as follows:
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 60 61 62 63 64 65 |
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.Attr; import org.w3c.dom.Document; import org.w3c.dom.Element; 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(); Document doc = buider.newDocument(); // Create <students> tag Element students = doc.createElement("students"); doc.appendChild(students); // Create <student> tag Element student = doc.createElement("student"); students.appendChild(student); // Create n0=1 attribute Attr attr = doc.createAttribute("n0"); attr.setValue("1"); student.setAttributeNode(attr); // Add <name> tag Element name = doc.createElement("name"); name.appendChild(doc.createTextNode("John")); student.appendChild(name); // <code> tag Element code = doc.createElement("code"); code.appendChild(doc.createTextNode("12345")); student.appendChild(code); // <age> tag Element age = doc.createElement("age"); age.appendChild(doc.createTextNode("19")); student.appendChild(age); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); File f = new File("E://students.xml"); StreamResult result = new StreamResult(f); transformer.transform(source, result); } } |
Result:
LH
Hello, i want to read xml file usind dom parser which containing firstname and lastname nodes then write output.xml file which have just one node ===> firstname =the values for ( firstname+lastname) .
input.xml
firstname : Jan
lastname : kruz
output.xml
fistname : jan kurz
how can i do it?