Solved

how to generate xml document form the table in a database

Posted on 2006-11-23
5
214 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
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now