Solved

XML Parsing Error: no element found - Using JDOM w/ Java Servlet

Posted on 2007-03-23
7
995 Views
Last Modified: 2012-06-27
I am getting the following error when I hit this servlet
ERROR:
XML Parsing Error: no element found
Location: http://url/LeagueStandings
Line Number 1, Column 1:

SERVLET CODE:
           RStoXML rsxml = new RStoXML(results, "ResultSet", "Result");
            //create the XML from recordset
            Document jDOMDoc = rsxml.build();            
            results.close();
            XMLOutputter outputter = new XMLOutputter() ;
            StringWriter writer = new StringWriter() ;
            outputter.output( jDOMDoc, writer );
            writer.close() ;
            //respond with output stream
            rep.setContentType("text/xml");
            rep.getOutputStream();
 logger.debug(outputter.outputString(jDOMDoc));

The XML returned in the debug message is below.
XML:
<?xml version="1.0" encoding="UTF-8"?>
<ResultSet>
    <Result>
         <ROUNDS>3</ROUNDS>
         <USERNAME>testuser</USERNAME>
         <BIRDIES>2</BIRDIES>
         <PARS>13</PARS>
         <BOGIES>28</BOGIES>
         <DOUBLES>11</DOUBLES>
         <GH>10</GH>
        <FH>10</FH>
        <PUTTS>108</PUTTS>
        <SCORES>264</SCORES>
    </Result>
</ResultSet>

0
Comment
Question by:fortneci
  • 4
  • 3
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 18783371
You can only use the outputtter once probably. Try just


rep.setContentType("text/xml");
outputter.output( jDOMDoc, rep.getWriter() );
0
 

Author Comment

by:fortneci
ID: 18783440
I put the  logger.debug(outputter.outputString(jDOMDoc)); in there after I experienced the error to check the XML, but I just took it out just in case and I am still getting the error.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18783464
Please post your current code
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:fortneci
ID: 18783471
The section of the Servlet:

            RStoXML rsxml = new RStoXML(results, "ResultSet", "Result");
            //create the XML from recordset
            Document jDOMDoc = rsxml.build();            
            results.close();
            XMLOutputter outputter = new XMLOutputter() ;
            StringWriter writer = new StringWriter() ;
            outputter.output( jDOMDoc, writer );
            writer.close() ;
            //respond with output stream
            rep.setContentType("text/xml");
            rep.getOutputStream();
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 18783481
You're not writing anything to the output stream. Try

rep.getWriter().println(writer.toString());
0
 

Author Comment

by:fortneci
ID: 18783508
Thanks, that got rid of the error. Below is the final snippet of code.

            RStoXML rsxml = new RStoXML(results, "ResultSet", "Result");
            //create the XML from recordset
            Document jDOMDoc = rsxml.build();            
            results.close();
            XMLOutputter outputter = new XMLOutputter() ;
            StringWriter writer = new StringWriter() ;
            outputter.output( jDOMDoc, writer );
            writer.close() ;
            //respond with output stream
            rep.setContentType("text/xml");
            rep.getWriter().println(writer.toString());
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18784671
:-) If you don't need 'writer' again you should be able to do

RStoXML rsxml = new RStoXML(results, "ResultSet", "Result");
//create the XML from recordset
Document jDOMDoc = rsxml.build();            
results.close();
XMLOutputter outputter = new XMLOutputter() ;
rep.setContentType("text/xml");
outputter.output( jDOMDoc, rep.getWriter() );
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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.

770 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