2016-11-08 81 views
-2

我正在處理這個Web應用程序,並將從窗體捕獲的值傳遞給存儲過程。現在,存儲過程的一個參數接受一個XML字符串。該參數被稱爲「DetailOld」,我不知道如何將表單中的字符串值轉換爲XML字符串。 任何人都可以請幫我嗎?如何在Java中將字符串數據類型轉換爲XML格式?

@Override 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     // processRequest(request, response); 
     PrintWriter out1 = response.getWriter(); 
     out1.println("Hey"); 
     try { 
      // processRequest(request, response); 
      out1.println("Servlet "); 
      Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
     } catch (ClassNotFoundException ex) { 
      Logger.getLogger(Connectioncheck.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     Connection conn = null; 
       PreparedStatement myStmt = null; 
       int num = -1; 

     try { 
      conn = DriverManager.getConnection("jdbc:sqlserver://MPESA\\SQL2012;user=realm;password=friend;database=ElmaTest"); 
      if(conn!=null) 
      { 
       out1.print("Connection succesful"); 

       try{ 


       myStmt = conn 
        .prepareStatement("exec p_AddEditAccountCustomer ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?"); 

       if(myStmt!=null) 
       { 
       String mnumber = request.getParameter("mobilenumber"); 
       String mtitle = request.getParameter("Title"); 
       String fname= request.getParameter("firstname"); 
       String mName = request.getParameter("MiddleName"); 
       String lname = request.getParameter("LastName"); 
       String maddress = request.getParameter("address"); 
       String melmaid = request.getParameter("elmaid"); 
       String mcity = request.getParameter("City"); 
       String mId = request.getParameter("Idtype"); 
       String midno = request.getParameter("idno"); 
       String operatorid = "1"; 
       String BankId = "99"; 
       String Country = "Kenya"; 
       String customercountry = "1"; 
       String maccntid = request.getParameter("accountid"); 
       String accounttype="1"; 
       String cardnumber ="1"; 
       String mcurrency = request.getParameter("currency"); 
       String branchid = "1"; 
       String AliasName="1"; 

       String createdBy="1"; 
       String createdOn ="2016-11-02"; 
       String DetailOld = "1";`//this is what the procedure //expects as an XML string. I have used the default value 1 for testing //purpose.` 
       String DetailNew = "1"; 
       String ModuleId="2001"; 
       String eventId="1"; 
       myStmt.setString(1, mnumber); 
       myStmt.setString(2, mtitle); 
       myStmt.setString(3, fname); 
       myStmt.setString(4, mName); 
       myStmt.setString(5, lname); 
       myStmt.setString(6, maddress); 
       myStmt.setString(7, melmaid); 
       myStmt.setString(8,mcity); 
       myStmt.setString(9, mId); 
       myStmt.setString(10, midno); 
       myStmt.setString(11,operatorid); 
       myStmt.setString(12, BankId); 
       myStmt.setString(13, Country); 
       myStmt.setString(14, customercountry); 
       myStmt.setString(15, maccntid); 
       myStmt.setString(16, accounttype); 
       myStmt.setString(17, cardnumber); 
       myStmt.setString(18, mcurrency); 
       myStmt.setString(19, branchid); 
       myStmt.setString(20,AliasName); 
       myStmt.setString(21, createdBy); 
       myStmt.setString(22,createdOn); 
       myStmt.setString(23, DetailOld); 
       myStmt.setString(24, DetailNew); 
       myStmt.setString(25, ModuleId); 
       myStmt.setString(26,eventId); 
       myStmt.executeQuery(); 

       } 

       }catch (SQLException exc) { 
        exc.printStackTrace(); 
     } finally { 

        //out1.println("Done properly"); 
        response.sendRedirect("CustomerDetails.jsp"); 



        myStmt.close(); 
        conn.close(); 
     } 



















      } 

     } catch (SQLException ex) { 
      Logger.getLogger(Connectioncheck.class.getName()).log(Level.SEVERE, null, ex); 
     } 


    } 

XML格式 - 字符串detailold-

<Table1> 
    <CustomerID>1916507095</CustomerID> 
    <AccountType>BANK</AccountType> 
    <Country>kenya</Country> 
    <BankID>99</BankID> 
    <BankAccountID>001001000095</BankAccountID> 
    <TitleOfAccount>Mr</TitleOfAccount> 
    <FirstName>Pramod1</FirstName> 
    <MiddleName>Kb1</MiddleName> 
    <LastName>Sahoo</LastName> 
    <BranchID>000</BranchID> 
    <TypeOfID>Driving License</TypeOfID> 
    <IDNumber>276999</IDNumber> 
    <EmailID>[email protected]</EmailID> 
    <Address>Nairobi</Address> 
    <City>AWENDO</City> 
    <CardNumber>kenya</CardNumber> 
    <CurrencyID>KES</CurrencyID> 
    <AliasName>pramod</AliasName> 
</Table1> 

回答

0
  1. 您可以創建的getter setter方法(POJO)的Java類 - 說Detailold.java
  2. 填充與數據POJO對象,您從循環中獲取數據庫。 https://www.mkyong.com/java/jaxb-hello-world-example/

    -
  3. 使用JAXB

下面是一個例子,如何使用JAXB數據轉換成XML轉換轉換該對象到XML

相關問題