Solved

how to generate xml document form the table in a database

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse package explorer vs project explorer view 2 132
print map entry 34 69
hibernate insert example 13 29
CSV file parsing thru Java 13 31
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 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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

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