Create Nodes and Attributes with DOM

Create new package named data. In this package, create new xml file named student.xml as below:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<student>
</student>




Create new package, named demo. In this package, create new java class, named Main as below

package demo;

import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
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;

public class Main {

	private static void createStudent() {
		String xmlFile = "src\\data\\student.xml";
		try {

			DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
			Document document = documentBuilder.parse(xmlFile);

			// Get root node
			Element student = document.getDocumentElement();

			// Create id tag
			Element id = document.createElement("id");
			id.appendChild(document.createTextNode("st01"));
			student.appendChild(id);

			// Create fullName tag
			Element fullName = document.createElement("fullName");

			// Create firstName tag
			Element firstName = document.createElement("firstName");
			firstName.appendChild(document.createTextNode("PMK"));
			fullName.appendChild(firstName);

			// Create lastName tag
			Element lastName = document.createElement("lastName");
			lastName.appendChild(document.createTextNode("Lab"));
			fullName.appendChild(lastName);

			student.appendChild(fullName);

			// Create dob tag
			Element dob = document.createElement("dob");
			dob.setAttribute("pattern", "yyyy-MM-dd");
			dob.appendChild(document.createTextNode("2018-10-20"));
			student.appendChild(dob);

			// Display document
			String xmlContent = getXMLContent(document);
			System.out.println(xmlContent);

			// Save to XML file
			saveXMLContent(document, xmlFile);

		} catch (Exception e) {
			System.err.println(e.getMessage());
		}
	}

	private static String getXMLContent(Document document) {
		String result = "";
		try {
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			StringWriter stringWriter = new StringWriter();
			StreamResult streamResult = new StreamResult(stringWriter);
			DOMSource domSource = new DOMSource(document);
			transformer.transform(domSource, streamResult);
			result = stringWriter.toString();
		} catch (Exception ex) {
			result = "";
		}
		return result;
	}

	private static void saveXMLContent(Document document, String xmlFile) {
		try {
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			DOMSource domSource = new DOMSource(document);
			StreamResult streamResult = new StreamResult(xmlFile);
			transformer.transform(domSource, streamResult);
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
	}

	public static void main(String[] args) {

		createStudent();

	}

}
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<student>
	<id>st01</id>
	<fullName>
		<firstName>PMK</firstName>
		<lastName>Lab</lastName>
	</fullName>
	<dob pattern="yyyy-MM-dd">2018-10-20</dob>
</student>