File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes ResultsSet  to XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "ResultsSet  to XML" Watch "ResultsSet  to XML" New topic
Author

ResultsSet to XML

jonas okwara
Ranch Hand

Joined: Jun 22, 2004
Posts: 58

Hello All, I have spent several days trying to get this code to output to an xml format. Please I will appreciate if i am pointed to what i am doing wrong. The output works but it is not formatted in xml.

My code is below:

Thank You
Jonas

while (rs.next()) {
String firstname = rs.getString(1);
String lastname = rs.getString(2);
writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
writer.println("<employeeInfo>");
writer.println("<contacts>");
writer.println("<FirstName>");
writer.println(firstname);
writer.println("</FirstName>");
writer.println("<LastName>");
writer.println(lastname);
writer.println("</LastName>");
writer.println("</contacts>");
writer.println("</employeeInfo>");[/b]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

That doesn't look too bad to me. What makes you think the output isn't well-formed XML?

(I can see several potential problems but I don't see any point in guessing when you could just provide a description of the problem.)
jonas okwara
Ranch Hand

Joined: Jun 22, 2004
Posts: 58

Like I said there were no errors but I see something like this:

jonas smith sandra smith john tandi micheal lowell

I expect to see this:
<employee>
<contact>
<firstname>jonas</firstname>
<lastname>smith</lastname>
</contact>
</employee>

etc.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

When you say "see", are you perhaps using a browser to display that document as HTML? Try "seeing" it in some more usual way, like outputting it to a file or the console.
jonas okwara
Ranch Hand

Joined: Jun 22, 2004
Posts: 58

Yes I am using a browser
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Well there you go then. The browser assumes it's HTML, and any tags which aren't HTML it just ignores them.
jonas okwara
Ranch Hand

Joined: Jun 22, 2004
Posts: 58
Thanks everyone. I was able to figure it out with the following code:

writer.println("<candidateInfo>");

while (rs.next()) {

writer.println("Firstname" + rs.getString(1) + "Firstname\n");
writer.println("Lastname" + rs.getString(2) + Lastname\n");
writer.println("Level" + rs.getString(3) + "Level\n");
writer.println("Score" + rs.getString(4) + "Score\n");
}

writer.println("candidateInfo");

This is now my Output:

<employeeInfo>
<Firstname>Chiemeka</Firstname>
<Lastname>Okwara</Lastname>
<Phone>2402067545</Phone>
<State>MD</State>
<Firstname>Adadadi</Firstname>
<Lastname>Okwara</Lastname>
<Phone>2402067545</Phone>
<State>MD</State>
<Firstname>Nneoma</Firstname>
<Lastname>Olisa</Lastname>
<Phone>2402067545</Phone>
<State>MD</State>
</employeeInfo>

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ResultsSet to XML
 
Similar Threads
JSP mySQL exception
Array and mapping
XSL/Xpath help needed
Calling another servlet
Generating XML from template file.