Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to generate xml document form the table in a database

Posted on 2006-11-23
5
Medium Priority
?
233 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 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

722 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