How to create XML file in Java

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.

2- Output

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

3- Source Code

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

P.S: Refer to How to read XML file in Java to check how to read and parse an existing XML using DOM parser.


5 3 votes
Article Rating

Hussein Terek

Owner of, I have a passion for software engineering and everything related to Java environment.

Inline Feedbacks
View all comments