creating document in XML

creating document in XML

View Answers

March 23, 2009 at 12:10 AM

Hi friend,

Code to help in solving the problem :

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);
}
}

Thanks

Ads









Related Tutorials/Questions & Answers:
creating document in XML - XML
creating document in XML  Create an XML document for catalogue...)); System.out.print("Enter number to add elements in your XML file: "); String... = documentBuilderFactory.newDocumentBuilder(); Document
creating index for xml files - XML
creating index for xml files  I would like to create an index file for xml files which exist in some directory. Say, my xml file is like below: smith 23 USA john 25 USA ... ... All xml files in the directory have
Advertisements
xml document parsing
xml document parsing   Hi Guys, Xml document has been inserted into the database,how do i parse the xml document stored in database,so that xml document can be retrieved on to JSP file using jdbc/odbc connection. Please help me
retrieving xml document from database
retrieving xml document from database  Hi Guys, I want to retrieve the xml document stored in the database on to a jsp page using jdbc/odbc connnection.Please help me in implementing this feature. Thank You Madhu
Validating XML document with a DTD
Validating XML document with a DTD If an xml document is well formed i.e.... an XML document valid it must be validated, or verified, against a DTD. DTD... as valid xml document. Validation of xml is optional. If you require the xml file
Question about parsing XML document with DOM in JAVA.
Question about parsing XML document with DOM in JAVA.  Hi, I need to parse a XML file witch has info about classes. So i have for each class to have... it with multiple for loops, one iside the other but it doesnt work. My XML is this below
java code to create xml document from DOM object
java code to create xml document from DOM object  Hey! After knowing the way we can create DOM objects and add elements to it-> then displaying it on the console ;is there a way I can output the same in xml document
how to develope an xml document with existing documents's body content?
how to develope an xml document with existing documents's body content?  Hi, i'm working with content optimization system.I want to know how we can take all data from an xml doc's body to develope another xml
XML Schema
. They are extensible as they are written in XML Simple xml document, creating dtd and schema...: #FFFFFF; } XML Schema An XML Schema defines how to structure an XML document... occur in the xml document. 2. The child elements for an elements 3. The order
problem in creating web application using servelet, jsp, jdbc and xml - JSP-Servlet
problem in creating web application using servelet, jsp, jdbc and xml  Using Servlet, JSP, JDBC and XML create a web application for a courrier company to provide online help in tracking the delivery status of items.  
Creating a DTD
.style1 { background-color: #FFFFFF; } Creating a DTD A document type definition (DTD) is used to validate XML files that reference the DTD file... etc for any XML file and defines constraints for how each element, attribute
Hello World XML
to structure it. This tutorial explains creating xml and how to use css on it. You can use css to present xml data with styles. Creating XML: 1. Open any text... line in your xml document and save the file. <?xml-stylesheet type="text
XML Schema
document. The XML Schema language is also referred to as XML Schema Definition...XML Schema           Introduction to XML Schema XML Schema is a W3C Standard
xml
for providing uniquely named elements and attributes in an XML document... document, just like a DTD. An XML Schema defines user-defined integrants like elements, sub-elements and attributes needed in a xml document. It defines the data
DOM Example For Creating a Child Node 
DOM Example For Creating a Child Node  In this section we will discuss about how to add a Child Node in Dom document. The addition of child node is a very logical. To add a child node first we create blank DOM document and then we
XML DTD
XML DTD  What is DTD in xml file ?   A DTD (Document Type Definition) defines the structure and building blocks of a xml document. It can be also written inside the xml document
Document Object Model(DOM)
Document Object Model(DOM)       The Document Object Model (DOM) is a an standard object model that represents HTML or XML and related formats. Document Object Model is platform
XML
XML  How i remove a tag from xml and update it in my xml
Creating files in jpa project
Creating files in jpa project       After creating "JPACRUDApplication"... "META-INF" folder and ritht click on it. Select New => XML
Creating an Applet
Creating an Applet  Hi, I have get a task regarding Creating an Applet using all Layout Manager, can anyone help me in this regard. I donā??t know how to make and please help me. Thanks.   To answer of your query
xml
xml  why the content written in xml is more secure
xml
xml  validate student login using xml for library management system
xml
xml  validate student login using xml for library management system
Creating Views
Creating Views Struts provides their own JSP tag library for creating view. For using those library you need to import them on your page as <%@taglib... data by model and handle them appropriately. For creating a view you should
XML
XML  please tell me how i remove one tag out of all similar type of tags in xml
XML
XML  create flat file with 20 records. Read the records using xml parser and show required details
xml
xml  how can i remove white space and next line when i copy stream to xml file
xml
xml  what is xml   Extensible Markup Language (XML... that is both human-readable and machine-readable. It is defined in the XML 1.0... gratis open standards. The design goals of XML emphasize simplicity, generality
Creating Exception
Creating Exception  class Myexception extends Exception{ private int detail; Myexecption(int a){ detail=a; } public String toString(){ return "Myexception["+detail+"]"; } } class Exceptiondemo{ static void Compute(int a) throws
XML, XML Tutorial, XML Tutorial Online, XML Examples, XML Tutorial Example
? Uses of XML Hello World XML Introduction to XML Document Comparing XML... Validating XML document with a DTD XML Schema The Purpose... an XML document using JDOM | Parsing The XML File Using JDOM
Java Generate XML Example
(document); An example for creating the XML in Java is given below package... elements and add it into the document // Creating a root element Element... rootElement.appendChild(ChildElement); 7. Create the output formate // Creating XML output

Ads