Image of How to create XML file in Java

ADVERTISEMENT

Table of Contents

Introduction

In this tutorial, we show how to create a new XML file using DOM parser provided by JDK.

We’re going to create an XML called students.xml which lists students along with their basic information.

1- Create XML using Transformer

The following code snippet appends 3 student elements to a root element called students and then transforms the document object to a pretty XML file using transform() method of Transformer.

private static void createPrettyXMLUsingDOM()
{
    try
    {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.newDocument();
        // students root element
        Element rootElement = doc.createElement("students");
        doc.appendChild(rootElement);
        
        // student elements
        for(int i=1; i<=3; i++)
        {
            Element student = doc.createElement("student");
            
            Element studentName = doc.createElement("name");
            studentName.setTextContent("Hussein " + i);
            
            student.setAttribute("id", String.valueOf(i));
            student.appendChild(studentName);
            
            rootElement.appendChild(student);
        }
        
        // Write the content into XML file
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File("students-new.xml"));
        
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        // Beautify the format of the resulted XML
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.transform(source, result);
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
    }
}

2- Output

After executing the above code, you should get the following XML:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<students>
    <student id="1">
        <name>Hussein 1</name>
    </student>
    <student id="2">
        <name>Hussein 2</name>
    </student>
    <student id="3">
        <name>Hussein 3</name>
    </student>
</students>

3- Source Code

You can download the source code from this repository: Read-XML

Summary

In this tutorial, we show how to create a new XML file using DOM parser provided by JDK.

Next Steps

If you're interested in learning more about the basics of Java, coding, and software development, check out our Coding Essentials Guidebook for Developers, where we cover the essential languages, concepts, and tools that you'll need to become a professional developer.

Thanks and happy coding! We hope you enjoyed this article. If you have any questions or comments, feel free to reach out to jacob@initialcommit.io.

Final Notes