Issue Req Storing Result Set Data in a XML File

Discussion in 'JSP' started by sathya_k_83, Sep 22, 2007.

  1. sathya_k_83

    sathya_k_83 New Member

    Joined:
    Sep 22, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    i am developing a project using jsp & servlet.
    i want*create a xml from result set using document builder but when the servlet is executed it displays the Http Status 500 error
    tell me what to do . where to place that xml file
    is there other for writing result set data as xml
    below is the code that* does not work
    Code:
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    Document mapDoc;
    Document dataDoc=null;
    String XmlFileName=request.getParameter("filename");
    DocumentBuilder db;
    DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
    System.out.println(XmlFileName);
    try {
    db=dbf.newDocumentBuilder();
    mapDoc=db.parse(XmlFileName);
    dataDoc=db.newDocument();
    } catch (ParserConfigurationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SAXException e) {
    // TODO Auto-generated catch block
    System.out.println("sax");
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    System.out.println("IO ->");
    e.printStackTrace();
    }
    Connection con;
    Statement st;
    ResultSet rs;
    ResultSetMetaData rsd=null;
    Element DocRoot=null;
    String query="select * from StationarRequest";
    try {
    Class.forName(DbConnect.getDriverName());
    con=DriverManager.getConnection(DbConnect.getDsnName(),DbConnect.getUid(),DbConnect.getPwd ());
    st=con.createStatement();
    rs=st.executeQuery(query);
    rsd=rs.getMetaData();
    DocRoot=dataDoc.createElement("StationaryDb");
    while(rs.next())
    {
    Element row=dataDoc.createElement("Stationary");
    for(int i=0;i<rsd.getColumnCount();i++)
    {
    String colName=rsd.getColumnName(i);
    String colType=rsd.getColumnTypeName(i);
    System.out.println(colType);
    String colValue="";
    if(colType.equals("String"))
    {
    colValue=rs.getString(i);
    }
    else if(colType.equals("int"))
    {
    colValue=rs.getInt(i)+"";
    }
    else if(colType.equals("float"))
    {
    colValue=rs.getFloat(i)+"";
    } 
    else if(colType.equals("double"))
    {
    colValue=rs.getDouble(i)+"";
    }
    Element DataCol=dataDoc.createElement(colName);
    DataCol.appendChild(dataDoc.createTextNode(colValue));
    row.appendChild(DataCol); 
    }
    DocRoot.appendChild(row); 
    }
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    System.out.println("class");
    e.printStackTrace();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    System.out.println("sql");
    e.printStackTrace();
    }
    dataDoc.appendChild(DocRoot);
    System.out.println("The Result Set is written into XML File");
    }
     
    Last edited by a moderator: Sep 22, 2007
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,376
    Likes Received:
    388
    Trophy Points:
    83
    I would suggest you should be putting the code that is you think could be a problem and also have the code well formatted for others to understand. At least I would not see your code if its as its in the above post.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice