Last Updated:

How to create an XML file in Java: using the DOM Parser

First, let's define a list of what will be in our xml file:

  1. The root element will be "Language" with the namespace "". This root element will contain a list of programming languages.
  2. Language information will be recorded in the Language element. In total, our XML file will contain information about languages.
  3. Each language has an attribute named "id«
  4. The language element will have two elements – "name", "age".

The following is the program that creates the Xml file described above:

Notice that two objects have been created in our program: one to print the generated XML content to the console for debugging and the other to write to an XML file.StreamResult

The result of the java XML file creation program using DOM Parser:

As you can see, the result of the first went to output information to the console, and the other created a new XML file .. /languages.xml.StreamResult