title: Returning XML from query result in servlet
body: <p>I'm trying to return an XML file based on my query results. I'm very new to this so I'm not really sure where I'm going wrong. Is this a realistic way to go about doing this or is there something simpler? Right now I'm getting these exceptions:</p>
  
  <pre><code>Error performing query: javax.servlet.ServletException: org.xml.sax.SAXParseException: Content is not allowed in prolog.
  </code></pre>
  
  <p>If I run my query in isql*plus, it does execute</p>
  
  <pre><code>import java.io.*;
  import java.util.*;
  import java.sql.*;           // JDBC packages
  import javax.servlet.*;
  import javax.servlet.http.*;
  import javax.xml.parsers.*;
  import org.xml.sax.*;
  import org.xml.sax.helpers.*;
  
  public class step5 extends HttpServlet {
  
     public static final String DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
     public static final String URL = "jdbc:odbc:rreOracle";
     public static final String username = "cm485a10";
     public static final String password = "y4e8f7s5";
  
     SAXParserFactory factory;
  
     public void init() throws ServletException {
  
        factory = SAXParserFactory.newInstance();
     }
  
     public void doGet (HttpServletRequest  request,
                        HttpServletResponse response)
        throws ServletException, IOException
        {
        PrintWriter out = response.getWriter();
        Connection con = null;
  
        try {
  
              Class.forName(DRIVER);
  
              con = DriverManager.getConnection(URL,username,password);
  
              try {
                 Statement stmt = con.createStatement();
                 ResultSet rs = stmt.executeQuery("SELECT sale_id, home_id, agent_id, customer_id FROM sale");
  
                 String xml = "";
                 xml = xml + "&lt;sales_description&gt;";
                 xml = xml + "&lt;sale&gt;";
  
                 boolean courseDataDone = false;
                 while (rs.next()) {
                    String sale_id = rs.getString(1);
                    String home_id = rs.getString(2);
                    String agent_id = rs.getString(3);
                    String customer_id = rs.getString(4);
  
                    if (!courseDataDone) {
                       xml = xml + "&lt;sale_id&gt;" + sale_id + "&lt;/sale_id&gt;" +
                                   "&lt;home_id&gt;" + home_id + "&lt;/home_id&gt;" +
                                   "&lt;agent_id&gt;" + agent_id + "&lt;/agent_id&gt;" +
                                   "&lt;customer_id&gt;" + customer_id + "&lt;/customer_id&gt;" +
                                   "" +
                                   "";
                       courseDataDone = true;
                    }
  
                 }
  
                 xml = xml + "&lt;/sale&gt;" +
                             "&lt;/sales_description&gt;";
  
                 try {
                    SAXParser parser = factory.newSAXParser();
                    InputSource input = new InputSource(new StringReader(xml));
                    parser.parse(input, new DefaultHandler());
                 } catch (ParserConfigurationException e) {
                    throw new ServletException(e);
                 } catch (SAXException e) {
                    throw new ServletException(e);
                 }
  
                 response.setContentType("text/xml;charset=UTF-8");
                 out.write(xml);
  
              } catch(Exception ex) {
                 out.println("Error performing query: " + ex);
                 con.close();
                 return;
              }
  
           } catch(Exception ex) {
              out.println("Error performing DB connection: " + ex);
              return;
           }
  
     }
  }
  </code></pre>
  
  <p>Any help/tips would be appreciated.</p>