To create XML file in Java,here we are using DOM parser. By using the below code you can easily create XML file.
Write the below Java class to create a XML file:
CreateXMLFile.java
package com.babita;
import java.io.File;
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;
public class CreateXMLFile {
public static void main(String args[]) {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
// root elements
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("company");
doc.appendChild(rootElement);
// employee elements
Element employee = doc.createElement("employee");
rootElement.appendChild(employee);
// set attribute to employee element
Attr attr = doc.createAttribute("id");
attr.setValue("1");
employee.setAttributeNode(attr);
// you can also use the below way toset attribute to employee element
// employee.setAttribute("id", "1");
// firstname elements
Element firstname = doc.createElement("firstname");
firstname.appendChild(doc.createTextNode("babita"));
employee.appendChild(firstname);
// lastname elements
Element lastname = doc.createElement("lastname");
lastname.appendChild(doc.createTextNode("dhami"));
employee.appendChild(lastname);
// emailId elements
Element emailid = doc.createElement("emailid");
emailid.appendChild(doc.createTextNode("babita.dhami@gmail.com"));
employee.appendChild(emailid);
// designation elements
Element designation = doc.createElement("designation");
designation.appendChild(doc.createTextNode("software engineer"));
employee.appendChild(designation);
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("C:\\myDetail.xml"));
// Output to console for testing, if you want see output on console
// StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
System.out.println("XML file created!");
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
}
}
}
Output:
The following XML file named myDetail.xml will be created after running the above example.
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<company>
<employee id="1">
<firstname>babita</firstname>
<lastname>dhami</lastname>
<emailid>babita.dhami@gmail.com</emailid>
<designation>software engineer</designation>
</employee>
</company>
Hope this will help you :)
0 Comment(s)