By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,572 Members | 963 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,572 IT Pros & Developers. It's quick & easy.

Store query result in a xml file

P: 5
I want to store the query result, in an servlet, into an xml file and display the contents of the xml file on the browser......

my code is as follows:

Expand|Select|Wrap|Line Numbers
  1. public void doPost(HttpServletRequest request, HttpServletResponse response)
  2.                                    throws ServletException,IOException{
  3. //      List dataList = new ArrayList();
  4.       response.setContentType("text/html");
  5.     PrintWriter pw = response.getWriter();
  6.  
  7.    String contextPath = request.getContextPath();
  8.    HttpSession session = request.getSession(true);
  9.  
  10.  String branchId = null;
  11.  String orgId = null;
  12.  String stateId = null;
  13.  String regionId = null;
  14.  String lot_ref="";
  15.  String lot_wt="";
  16.  String item="";
  17.  
  18.  
  19.  
  20.  
  21.   PrintWriter out = response.getWriter();
  22.     //Establish connection to MySQL database
  23.      String url="jdbc:mysql://192.168.1.3:3306/APMC?user=apmc&password=apmc";
  24.     Connection con = null;
  25.     ResultSet rs=null;
  26.     Statement st=null;
  27.     try {
  28.  
  29.  
  30.        Class.forName("com.mysql.jdbc.Driver").newInstance();
  31.        System.out.println("driver regristration success!");
  32.        con=DriverManager.getConnection(url);
  33.        System.out.println("connected tendring............     ");
  34.  
  35.       String sql = "select SQL_CALC_FOUND_ROWS lot.n_lot_ref_no,bidact.c_item_name,"+
  36.                             " bidact.m_lot_wieght FROM t_bidding_activation bidact, t_lot_master lot, t_stakeholder_master stakeholder"+
  37.                            " where bidact.c_active='Y' and "+
  38.                             "bidact.n_lot_id=lot.n_lot_id and stakeholder.n_stakeholder_id=lot.n_stakeholder_id  and bidact.c_bidded!='Y'"+
  39.                             "and bidact.c_branch_id='" + branchId + "' and bidact.c_org_id='"+ orgId +"' and bidact.c_state_id='" + stateId + "' "+
  40.                             "and bidact.c_region_id='" + regionId + "'  order by bidact.c_item_name,"+
  41.                             "bidact.d_bid_end_date, bidact.d_bid_end_time";
  42.  
  43. System.out.println("Output from dopost..........................");
  44. System.out.println("after query..................................");
  45.       st = con.createStatement();
  46.       st.executeQuery (sql);
  47.       rs = st.getResultSet();
  48.       System.out.println("result" +rs);
  49.  
  50.       System.out.print("before while");
  51.       while (rs.next()){
  52.         //Add records into data list
  53.           System.out.println("Hello....................................");
  54.         lot_ref= rs.getString(1);
  55.         System.out.print("refnum"+lot_ref);
  56.         lot_wt=rs.getString(2);
  57.        System.out.print("lotwt"+lot_wt);
  58.         item= rs.getString(3);
  59.        System.out.print("item"+item);
  60. //       dataList.add(rs.getString("lot.n_lot_ref_no"));
  61. //        dataList.add(rs.getString("bidact.c_item_name"));
  62. //        dataList.add(rs.getString("bidact.m_lot_wieght"));
  63.  
  64.  
  65.       }
  66. System.out.print("after while");
  67.  
  68.  
  69.  
  70.  
  71.       }catch(Exception e){
  72.       System.out.println("Exception is ;"+e);
  73.       }
  74.  
  75.  
  76.  
  77.   /*if(request.getParameter("strUserName")!=null && request.getParameter("strPassword")!=null)
  78.     {
  79.         String username = request.getParameter("strUserName").toString();
  80.        String passwd = request.getParameter("strPassword").toString();
  81.     }*/
  82.     // if (uName.equals(userName) && passwd.equals(password)){
  83.  
  84.   try
  85.   {
  86.     DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
  87.         DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
  88.         //creating a new instance of a DOM to build a DOM tree.
  89.         Document doc = docBuilder.newDocument();
  90.         new xmlServlet().createXmlTree(doc,lot_ref, lot_wt,item);
  91.  
  92.  
  93.     pw.println("<b>Xml File Created Successfully</b>");
  94.   }
  95.   catch(Exception e)
  96.   {
  97.     System.out.println(e);
  98.   }
  99.     try{
  100.     rs.close ();
  101.       st.close ();
  102.       con.close();
  103.     }catch(Exception e){
  104.       e.printStackTrace();
  105.     }
  106.     /* }
  107.      else
  108.      {
  109.      System.out.println("Invalid User");
  110.      }*/
  111.  
  112.   }
  113.  
  114.   public void createXmlTree(Document doc, String lot_ref, String lot_wt, String item) throws Exception {
  115.         //This method creates an element node
  116.         Element root = doc.createElement("User");
  117.         //adding a node after the last child node of the specified node.
  118.         doc.appendChild(root);
  119.  
  120.  
  121.  
  122.         Element child1 = doc.createElement("lot_ref");
  123.         root.appendChild(child1);
  124.  
  125.         Text text = doc.createTextNode(lot_ref);
  126.         child1.appendChild(text);
  127.  
  128.  
  129.  
  130.         Element element = doc.createElement("lot_wt");
  131.         root.appendChild(element);
  132.  
  133.         Text text1 = doc.createTextNode(lot_wt);
  134.         element.appendChild(text1);
  135.  
  136.         Element element1 = doc.createElement("item");
  137.         root.appendChild(element1);
  138.  
  139.         Text text2 = doc.createTextNode(item);
  140.         element.appendChild(text2);
  141.  
  142.  
  143.  
  144.  
  145.      //TransformerFactory instance is used to create Transformer objects.
  146.         TransformerFactory factory = TransformerFactory.newInstance();
  147.         Transformer transformer = factory.newTransformer();
  148.  
  149.         transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  150.  
  151.  
  152.         // create string from xml tree
  153.         StringWriter sw = new StringWriter();
  154.         StreamResult result = new StreamResult(sw);
  155.         DOMSource source = new DOMSource(doc);
  156.         transformer.transform(source, result);
  157.         String xmlString = sw.toString();
  158.  
  159.         File file = new File("c:/temp/tender.xml");
  160.         BufferedWriter bw = new BufferedWriter
  161.                       (new OutputStreamWriter(new FileOutputStream(file)));
  162.         bw.write(xmlString);
  163.         bw.flush();
  164.         bw.close();
  165.  
  166.     }
  167.  
  168.  
  169. }
Here I am able to create a xml file ... but i need to use some array ... to store large number of data... which i am not able to do.....
could u help me regarding this,
once i do tat ... i need to display the content of the xml file in the browser.

regards,
reebeca
Mar 24 '10 #1
Share this Question
Share on Google+
2 Replies


Dheeraj Joshi
Expert 100+
P: 1,123
First of all, you have posted your question in the wrong place.

Have you tried to code? What problem you are getting?

Regards
Dheeraj Joshi

* When i was typing this question Dormilich moved this thread
Mar 24 '10 #2

jkmyoung
Expert 100+
P: 2,057
It seems you can create each row in the while(rs.next()) loop. Why not add your nodes there? Have a function kind of like createXmlTree(), say instead createXMLBranch(), and add the node created to your main document node.

Also, let's focus on making the xml file before you get caught up in display.
Mar 25 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.