Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa XML creating tables as an xml document

Have Programming Question? Ask it here!
 
 
 


Shridhu Chandel
creating tables as an xml document
1 Answer(s)      5 years and 5 months ago
Posted in : XML



Advertisement
View Answers

March 21, 2009 at 4:03 AM


Hi friend,

Code to help in solving the problem :

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

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

public class XmlServlet extends HttpServlet{

public static void main(String args[]) throws Exception
{

try
{
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
//creating a new instance of a DOM to build a DOM tree.
Document doc = docBuilder.newDocument();
new XmlServlet().createXmlTree(doc);

System.out.println("<b>Xml File Created Successfully</b>");
}
catch(Exception e)
{
System.out.println(e);
}

}

public void createXmlTree(Document doc) throws Exception {
//This method creates an element node

Element root = doc.createElement("olympic_games");
//adding a node after the last child node of the specified node.
doc.appendChild(root);

Element child = doc.createElement("medal_tally");
root.appendChild(child);

Element child1 = doc.createElement("country_name");
child.appendChild(child1);

Text text = doc.createTextNode("USA");
child1.appendChild(text);

Element element = doc.createElement("gold_medal");
child.appendChild(element);

Text text1 = doc.createTextNode("8");
element.appendChild(text1);


Element silver_element = doc.createElement("silver_medal");
child.appendChild(silver_element);

Text text2 = doc.createTextNode("9");
silver_element.appendChild(text2);


Element bronze_element = doc.createElement("bronze_element");
child.appendChild(bronze_element);

Text text3 = doc.createTextNode("10");
bronze_element.appendChild(text3);







//TransformerFactory instance is used to create Transformer objects.
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();

transformer.setOutputProperty(OutputKeys.INDENT, "yes");

// create string from xml tree
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
String xmlString = sw.toString();

File file = new File("c:/newxml.xml");
BufferedWriter bw = new BufferedWriter
(new OutputStreamWriter(new FileOutputStream(file)));
bw.write(xmlString);
bw.flush();
bw.close();

}
}

Thanks


Related Tutorials/Questions & Answers:
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
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
Advertisements
Creating tables - IDE Questions
Creating tables  can i get a code that displays 10 records per page from a table student and when i click on the next button it displays the next 10 pages and so on, i'm using vb.net and sql database
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
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 Database Tables and Indexes
Creating Database Tables and Indexes       In this section you will learn about describing the allowable syntax for identifiers in MySQL. The identifiers are Database, table, index
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
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 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      ... to Parsing XML documents. With JAXP, we will use the Document BuilderFactory... xml document.   DocumentBuilderFactory uses the system property
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)
Create - XML File (Document)     ... document using the DOM APIs. This XML document uses  1.0 version  and UTF-8 encoding.  Description of program: This program helps in creating a XML
create the SQL tables from an XML schema
create the SQL tables from an XML schema  I'm using Eclipse, Please let me know if anyone developped an application that would automatically create the SQL tables from an XML schema
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... element from the XML document by  invoking the getParentNode() method . Here
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
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...; the file using the parse() method. This method parses the XML document
How to Generate XML files from the data in DB tables?
How to Generate XML files from the data in DB tables?  HI Experts, I have a doubt. I have a table say Pest. My task is to generate XML file with all the fields in table and store it to database.Please help me.I am new to XML
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
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
Getting Data from XML File (Document)
Getting Data from XML File (Document)   ... to the output . Here the results are displayed at the console from the XML document... from a XML file. All xml files store the data. You can add and modify the data
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
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 ... retrieved from the xml document.  An object of Document is passed
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
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
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
Nest tables within tables
Nest tables within tables  Can I nest tables within tables
DTD:Document Type Definition
of an XML document. It defines the document structure with a list of legal ...; If the DTD is defined inside the XML document, it should be wrapped in a DOCTYPE... of a XML document with an internal DTD: E-mail.xml <?xml
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... the validity of XML documents are DTDs  and the Schemas. DOM: Document Object Model
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
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.  
tables for layout
tables for layout  Are there any problems with using tables for layout
tables for layout
tables for layout  Are there any problems with using tables for layout
CSV tables
CSV tables  hello, What are CSV tables?   hii, CSV Tables cannot be indexed.CSV Tables are the special tables, data for which is saved into comma-separated values files
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
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
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
Comparing tables
Comparing tables  How to compare two or more tables in the Mysql database using jdbc
Hibernate 4 Many to Many Mapping using Xml
In this section, you will learn how to do many to many mapping of tables in Hibernate using Xml
include' a HTML document in another document.
include' a HTML document in another document.  I'm trying to `include' a HTML document in another document...Is there a way to do
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
DOM Parser Example to Parse A DOM Document
DOM Parser Example to Parse A DOM Document In this tutorial, we will discuss about how to parse(retrieve data) DOM Document. The XML DOM views an XML document as a tree-structure. In this program, we read the XML file and print
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
XML Syntax Rules
            Let's develop a simple XML document...; Always the first line in the xml document:  The XML declaration should... document contains a  E-mail To Rohan  From Amit. Don't you agree that XML
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
Getting The XML Root Element
a  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: You need a XML document (file). Both
Advertisements
 

 

 

DMCA.com