Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Generate The XML File

This Java program allows the user to create an XML file by prompting them to enter the root element name, number of child elements to add, each child element name, and data. It uses DOM and Transformer APIs to build the XML document in memory with the user-provided input and output the completed XML file to the console.

Uploaded by

govindbirajdar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Generate The XML File

This Java program allows the user to create an XML file by prompting them to enter the root element name, number of child elements to add, each child element name, and data. It uses DOM and Transformer APIs to build the XML document in memory with the user-provided input and output the completed XML file to the console.

Uploaded by

govindbirajdar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

package s; import java.io.*; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.*; import javax.xml.transform.stream.*; import org.w3c.dom.

*;

public class CreatXMLFile { public static void main(String[] args) throws Exception { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter number to add elements in your XML file: "); String str = bf.readLine(); int no = Integer.parseInt(str); System.out.print("Enter root: "); String root = bf.readLine(); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.newDocument(); Element rootElement = document.createElement(root); document.appendChild(rootElement); for (int i = 1; i <= no; i++){ System.out.print("Enter the element: ");

String element = bf.readLine(); System.out.print("Enter the data: "); String data = bf.readLine(); Element em = document.createElement(element); em.appendChild(document.createTextNode(data)); rootElement.appendChild(em); } TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(System.out); transformer.transform(source, result); } }

You might also like