Java Program To Create XML File

Chapter: Miscellaneous Last Updated: 23-04-2023 04:50:46 UTC

Program:

            /* ............... START ............... */
                

import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class XMLCreator {
    public static void main(String[] args) {
        try {
            // Create a new DocumentBuilderFactory
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

            // Create a new DocumentBuilder
            DocumentBuilder builder = factory.newDocumentBuilder();

            // Create a new Document
            Document doc = builder.newDocument();

            // Create the root element
            Element root = doc.createElement("root");
            doc.appendChild(root);

            // Create a child element and add it to the root
            Element child = doc.createElement("child");
            child.appendChild(doc.createTextNode("This is the child element"));
            root.appendChild(child);

            // Create another child element and add it to the root
            Element child2 = doc.createElement("child2");
            child2.appendChild(doc.createTextNode("This is the second child element"));
            root.appendChild(child2);

            // Write the Document to a file
            File file = new File("example.xml");
            javax.xml.transform.TransformerFactory.newInstance().newTransformer().transform(
                new javax.xml.transform.dom.DOMSource(doc),
                new javax.xml.transform.stream.StreamResult(file)
            );
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

                /* ............... END ............... */
        

Output

<root>
    <child>This is the child element</child>
    <child2>This is the second child element</child2>
</root>

Notes:

  • The Java program creates an XML file using the Java programming language.It starts by importing the necessary classes from the javax.xml.parsers and org.w3c.dom packages. These classes provide a way to work with XML data in Java.
  • The program then creates a new DocumentBuilderFactory and a new DocumentBuilder, which we use to create a new Document object that represents an XML document.
  • The program creates a root element called root using the createElement method of the Document object and appends it to the document using the appendChild method. It then creates two child elements called child and child2, sets their text content using the createTextNode method, and appends them to the root element using the appendChild method.
  • Finally, the program writes the Document object to a file called example.xml using the javax.xml.transform.TransformerFactory class, which provides a way to transform an XML document into various output formats.
  • The resulting XML file has a root element called root with two child elements called child and child2, each with their respective text content.
Similar Programs Chapter Last Updated
Find Unique Elements In List Java Miscellaneous 07-10-2023
Java Program To Implement A Custom equals() and hashcode() In Java Miscellaneous 07-10-2023
Java Program To Find The Intersection Of Two HashSets Miscellaneous 07-10-2023
Java Program To Remove Duplicate Elements From List Miscellaneous 07-10-2023
Java program to parse a date and time string from a log file and store it in a database Miscellaneous 19-09-2023
Java Program To Print All The Dates In A Month That Fall On A Weekend Miscellaneous 19-09-2023
Java Program To Find Number Of Working Days In A Month Miscellaneous 19-09-2023
Java Program To Calculate Age From Year Of Birth Miscellaneous 16-09-2023
How To Check If Two Strings Are Anagrams In Java Miscellaneous 22-08-2023
Java Program To Make A Snake Game Miscellaneous 15-08-2023
Java Program To Find Repeated Characters Of String Miscellaneous 15-08-2023
String To Array In Java Miscellaneous 11-08-2023
Java Program To Convert Date To String Miscellaneous 11-08-2023
Java Program To Convert String To Date Object Miscellaneous 11-08-2023
Java Program To Find Number Of Days In A Month Miscellaneous 11-08-2023
Java Program To Print First And Last Day Of Month Miscellaneous 11-08-2023
Java Program To Find Leap Year Between Two Dates Miscellaneous 11-08-2023
Java Code To Find Difference Between Two Dates In Years Months And Days Miscellaneous 11-08-2023
Java program to calculate age from year of birth Miscellaneous 29-06-2023
Swap Two Numbers Without Using Third Variable In Java Miscellaneous 02-06-2023
Java Program To Find The Average Of An Array Of Numbers Miscellaneous 02-06-2023
How Do You Find The Factorial Of A Number In Java Miscellaneous 02-06-2023
Java Program That Takes Two Numbers As Input And Prints Their Sum Miscellaneous 27-05-2023
How To Get The Length Of An Array In Java Miscellaneous 27-05-2023
Java Add Element To List Example Miscellaneous 19-05-2023
Java Program To Square All Items In List Miscellaneous 17-05-2023
Java Program To Merge Two Lists Miscellaneous 17-05-2023
How To Reverse A List In Java Miscellaneous 17-05-2023
Java Program To Find Unique Elements In An Array Miscellaneous 14-05-2023
Java Program To List All Elements In List Miscellaneous 30-04-2023

1 2