Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to generate xml document form the table in a database

Posted on 2006-11-23
5
Medium Priority
?
239 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
3 Comments
 
LVL 8

Accepted Solution

by:
reach2piyush earned 336 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 332 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 332 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

Independent Software Vendors: 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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

773 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