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









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 tables as an xml document - XML
creating tables as an xml document  Create a table of a medal tally of the Olympic Games as an XML document. The table must have the name...(); DocumentBuilder docBuilder = builderFactory.newDocumentBuilder(); //creating a new
Advertisements
Creating XMl file - XML
Creating XMl file   I went on this page: http://www.roseindia.net/xml/dom/createblankdomdocument.shtml and it shows me how to create an XML file, however there is something I don't understand. I have to create an XML file
creating Xml - Java Beginners
creating Xml  how we can create a xml file using java Creating XML... object. Creating XML File is possible  Creating XML File is possible... by Element Object.4. Appeng this Elemnts to DOcumnet object.  Creating XML
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
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
Creating Blank DOM Document
Creating Blank DOM Document      ... approach to Parsing XML documents. With JAXP, we will use the Document...); //The parse function is used to parse existing xml document.  ADS
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
Creating MS Word document in java - Struts
Creating MS Word document in java  I have one template in MS Word which has some content. Now i want to read this doc file and wants to add some... the doc file , do some changes , adding header footer and creating new doc file.Can
Create - XML File (Document)
in creating a XML document on the console. This program asks for the number... Create - XML File (Document)       In this section, you will  learn to create a XML document
Parsing XML using Document Builder factory - XML
Parsing XML using Document Builder factory  Hi , I am new to XML . I am trying to parse a XML file while is : For this i am using Document... org.w3c.dom import Document from org.w3c.dom import Element from
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
Reading an XML document using JDOM
Reading an XML document using JDOM       This Example shows you how to Read an XML document... for parsing, creating, manipulating, and serializing XML documents
Remove Element from XML Document
Remove Element from XML Document     ... a given  XML document. Whenever you remove the xml element from the xml document... the given element from the XML document by  invoking the getParentNode
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
Searching an Element in the given XML Document
Searching an Element in the given XML Document   ... in the specified XML document using DOM APIs defined... in the XML document. Use the getLength() method for counting the occurrences of a given
Creating XML Tree
Creating XML Tree       This Example shows you how to Create an XMLTree in a DOM document. JAXP (Java API for XML Processing) is an interface which provides parsing of xml documents
Creating a DocumentFragment Subtree and Appending it to the Document
Creating a DocumentFragment Subtree and Appending it to the Document... this fragment tree to a DOM document. JAXP (Java API for XML Processing) is an interface which provides parsing of xml documents. Here the 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
document, creating dtd and schema book.xmlADS_TO_REPLACE_3 <?xml version...: #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
how to parse xml document which contains elements with namespace using JDOM - XML
how to parse xml document which contains elements with namespace using JDOM  Hello, I am parsing XML file ,which has structure like... is inside so how can i parse this xml using JDOM,java. Thanks Shree  Hi
Getting Data from XML File (Document)
Getting Data from XML File (Document)   ... the XML document. Here is the XML File: Employee-Detail.xmlADS_TO_REPLACE_2... from a XML file. All xml files store the data. You can add and modify the data
Storing Data (Retrieved from a XML Document) to a File
Storing Data (Retrieved from a XML Document) to a File... to store data (retrieved from the XML document) to a specified file (with ... for generating the text retrieved from the xml document.  An object
JDOM CDATA Example, How to add CDATA section in xml document.
JDOM CDATA Example, How to add CDATA section in xml document. In this tutorial, we will see how to add CDATA section in xml document. In xml document...;xml ="Company.xml";   File  file=new File(xml
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
XML Parsers
; XML parser is used to read, update, create and manipulate an XML document. Parsing XML Documents To manipulate an XML document, XML parser is needed. The parser...: Document Object Model The XML Document Object Model (XML DOM) defines a standard way
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 Syntax Rules
_TO_REPLACE_3 Let's develop a simple XML document : <?xml version... document:  The XML declaration should always be included. It defines the XML version and the character encoding used in the document. In this case
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... you want. Add the following line in your xml document and save the file.ADS
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
XML - XML
. A user defines his own tags and XML document structure like Document Type...XML  What is specific definatio of XML? Tell us about marits and demarits of XML? use of XML? How we can use of XML?  Hi, XML
DTD:Document Type Definition
of an XML document. It defines the document structure with a list of legal ... Internal DTD  If the DTD is defined inside the XML document, it should be wrapped..._TO_REPLACE_2   Example of a XML document with an internal DTD: E-mail.xml
xml - XML
friend, XPath : It is a language for finding information in an XML document. It is used to navigate through elements and attributes in an XML document. It is a language for selecting nodes from an XML document. It is used
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
Getting The XML Root Element
; root element from the XML document.  The JAXP (Java APIs for XML Processing) provides a common interface for creating and using xml files using... interface.  Description of program:ADS_TO_REPLACE_1 You need a XML document
Open Source XML Editor
The Xerlin Project is a Java? based XML modeling application written to make creating... for developers interested in creating a UI for working with XML data files. Xerlin... is an XML editor that supports any XML document, and works with XML Schemas, DTDs
XML Well-Formed Verifier
that determines whether a  XML document is well-formed or not. Description... with the help of parse() method. The XMLReaderFactory helps in creating an XML reader which parses  xml document using the appropriate callbacks
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
xml developing - XML
xml developing  I want to develop XML document with following DTD file with XML format. I have also XSL file as a externatl file to represent my xml... creating final XML, if i make any modification to original xml (i.e. " operation
xml developing - XML
xml developing  I want to develop XML document with following DTD file with XML format. I have also XSL file as a externatl file to represent my xml... creating final XML, if i make any modification to original xml (i.e. " operation
XML Validate DTD
a xml file and its DTD document. First of all construct a well-formed xml file... file. After creating these, we parse the xml file using the parse() method... XML Validate DTD       
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
Eclipse Plunging/XML
, XML perspective, XSLT Debugger perspective, wizards, document templates... or by the document structure itself, the full support for DTD, XML Schema, Relax NG schema... in a URL (to an XML or HTML document) and an XPath expression, and it displays
xml - XML
xml  hi convert xml document to xml string.i am using below code...-an-xml-document-using-jd.shtml Hope that it will be helpful for you... = stw.toString(); after i am getting xml string result, like Successxxx profile
Reading XML from a File
a DOM document. JAXP (Java API for XML Processing) is an interface which provides... for the processing of XML Documents. Here the Document BuilderFactory is used... Reading XML from a File      
XML
XML  How i remove a tag from xml and update it in my xml
xml
xml  why the content written in xml is more secure
xml
xml  validate student login using xml for library management system

Ads