Solved

how to generate xml document form the table in a database

Posted on 2006-11-23
5
226 Views
Last Modified: 2010-03-31
Hi Experts,

If I have a table like

COLUMNS          FIRSTNAME         LASTNAME
                        abcd                    abcde
                        bcde                    bcdef

i want to generate an xml file like
<root>
<Names>
   <FIRSTNAME>abcd</FIRSTNAME>
   <LASTNAME>abcde</LASTNAME>
</Names>
<Names>
  <FIRSTNAME>bcde</FIRSTNAME>
  <LASTNAME>bcdef</LASTNAME>
</Names>
</root>

Please help out, regarding how to generate this using java code.
0
Comment
Question by:rnayeneni
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 8

Accepted Solution

by:
reach2piyush earned 84 total points
ID: 18005480
Here are multiple ways to do this, choose the one that suits your need:

1) Create the XML as a String like StringBuffer b = new StringBuffer("<root>")
then loop for the values and append b.append("<FIRSTNAME>").append("abcd").append("</FIRSTNAME>")
& so on..

2) Create Java class called Names with two elements FIRSTNAME & LASTNAME, Create a list of this class with all values in a list called as root, then use JAXB to create a XML document from it
Here is a link to download the JAXB API http://java.sun.com/webservices/jaxb/

3) Use other APIs like XERCES to create XML document, see the below tutorial for code sample (Section 6.2)
http://totheriver.com/learn/xml/xmltutorial.html#6.2
0
 
LVL 23

Assisted Solution

by:Ajay-Singh
Ajay-Singh earned 83 total points
ID: 18005814
try this (this solution is jdom based: http://www.jdom.org):


Connection connection = ... // create or get the connection.
        Statement statement = connection.createStatement();
        ResultSet result = statement.executeQuery("select * from table");
        Element root = new Element("root");
        while (result.next()) {
            Element node = new Element("Names");
            root.addContent(node);

            node.addContent(new Element("FIRSTNAME").setText(result.getString("FIRSTNAME")));
            node.addContent(new Element("LASTNAME").setText(result.getString("LASTNAME")));
        }

        Document doc = new Document(root);
        String generatedDocText = new XMLOutputter(Format.getPrettyFormat()).outputString(doc);
0
 
LVL 35

Assisted Solution

by:girionis
girionis earned 83 total points
ID: 18006922
Here is a compelte guide on how you can do it: http://www.developer.com/db/article.php/3329001
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question