2017-09-14 90 views
0

我想創建由同樣喜歡從數據庫這樣獲取數據後,下面給出一個XML文件:my db table looks like this我用的Java servlet是在XML文件創建新手任何幫助,將不勝感激。如何從mysql數據庫獲取數據後創建XML文件?

,我想通過一個servlet來創建我的XML文件:

<?xml version="1.0" encoding="UTF-8"?> 
     <root> 
     <expanded>true</expanded> 
    <children> 
    <element> 
     <text>Setup</text> 
     <leaf>false</leaf> 
     <iconCls>x-fa fa-gears</iconCls> 
     <cls>mainNav</cls> 
     <children> 
      <element> 
       <leaf>false</leaf> 
       <text>Academics</text> 
       <iconCls>x-fa fa-graduation-cap</iconCls> 
       <cls>mainNav</cls> 
       <children> 
      <element> 
       <leaf>true</leaf> 
       <text>Session</text> 
       <iconCls>x-fa fa-star</iconCls> 
       <cls>PIU.view.setup.academics.AcademicSession</cls> 
      </element> 
      <element> 
       <leaf>true</leaf> 
       <text>Faculty</text> 
       <iconCls>x-fa fa-star-o</iconCls> 
       <cls>PIU.view.setup.academics.AcademicFaculty</cls> 
      </element> 
      <element> 
       <leaf>true</leaf> 
       <text>Shifts</text> 
       <iconCls>x-fa fa-star-o</iconCls> 
       <cls>PIU.view.setup.academics.MaintainShifts</cls> 
      </element> 
      </children> 
      </element> 
      <element> 
       <leaf>false</leaf> 
       <text>Institution</text> 
       <iconCls>x-fa fa-university</iconCls> 
       <cls>mainNav</cls> 
      <children> 
      <element> 
       <leaf>true</leaf> 
       <text>Institution</text> 
       <iconCls>x-fa fa-globe</iconCls> 
       <cls>PIU.view.setup.institution.DefineInstitution</cls> 
      </element> 
      <element> 
       <leaf>true</leaf> 
       <text>Facilities</text> 
       <iconCls>x-fa fa-building</iconCls> 
       <cls>PIU.view.setup.institution.MaintainFacilities</cls> 
      </element> 
      <element> 
       <leaf>true</leaf> 
       <text>Letter Head</text> 
       <iconCls>x-fa fa-info</iconCls> 
      </element> 
      </children> 
       </element> 
      </children> 
      </element> 
     </children> 
    </root> 

到目前爲止,我已經試過這無法產生我想要的結果,請大家幫我出:

protected void doGet(HttpServletRequest request, HttpServletResponse 
    response) throws ServletException, IOException { 
    // TODO Auto-generated method stub 
    response.setContentType("text/xml"); 
    String reportName = "GenerateXML_Report_" 
      +System.currentTimeMillis()+".xml";  
    response.setHeader("Content-disposition", "attachment; " + 
      "filename=" + reportName); 

    try{  
     Datasource ds=new Datasource(); 
     ds.connect(); 
     java.sql.Statement s=ds.createStatement(); 
     ResultSet rs = s.executeQuery("Select label,action,icon_cls,leaf from common_features where is_visible=1"); 

    ArrayList<String> rows = new ArrayList<String>(); 
    rows.add("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 
    rows.add("<root>"); 
    rows.add("<expanded>true</expanded>"); 
    rows.add("<children>"); 
    while(rs.next()) 
    { 
     rows.add("<element>"); 
     rows.add("<leaf>"); 
     rows.add(rs.getString(4)); 
     rows.add("</leaf>"); 
     rows.add("<text>"); 
     rows.add(rs.getString(1)); 
     rows.add("</text>"); 
     rows.add("<iconCls>"); 
     rows.add(rs.getString(3)); 
     rows.add("</iconCls>"); 
     rows.add("<cls>"); 
     rows.add(rs.getString(2)); 
     rows.add("</cls>"); 
     rows.add("</element>"); 

    } 
    rows.add("</children>"); 
    rows.add("</root>"); 

    Iterator<String> iter = rows.iterator(); 
    while (iter.hasNext()){ 
     String outputString = (String) iter.next(); 
     response.getOutputStream().print(outputString); 
    } 
    ds.dropConnection(); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
    //out.println(jobject); 
    response.getOutputStream().flush(); 


} 

當前我生成的xml文件如下所示:

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
<expanded>true</expanded> 
    <children> 
    <element> 
     <leaf>true</leaf> 
     <text>Institution</text> 
     <iconCls>x-fa fa-globe</iconCls> 
     <cls>PIU.view.setup.institution.DefineInstitution</cls> 
    </element> 
    <element> 
     <leaf>true</leaf> 
     <text>Facilities</text> 
     <iconCls>x-fa fa-building</iconCls> 
     <cls>PIU.view.setup.institution.MaintainFacilities</cls> 
    </element> 
    <element> 
     <leaf>true</leaf> 
     <text>Session</text> 
     <iconCls>x-fa fa-star</iconCls> 
     <cls>PIU.view.setup.academics.AcademicSession</cls> 
    </element> 
    <element> 
     <leaf>true</leaf> 
     <text>Faculty</text> 
     <iconCls>x-fa fa-star-o</iconCls> 
     <cls>PIU.view.setup.academics.AcademicFaculty</cls> 
    </element> 
    <element> 
     <leaf>true</leaf> 
     <text>Shifts</text> 
     <iconCls>x-fa fa-star-o</iconCls> 
     <cls>PIU.view.setup.academics.MaintainShifts</cls> 
    </element> 
    <element> 
     <leaf>false</leaf> 
     <text>Setup</text> 
     <iconCls>x-fa fa-gears</iconCls> 
     <cls>mainNav</cls> 
    </element> 
    <element> 
     <leaf>false</leaf> 
     <text>Academics</text> 
     <iconCls>x-fa fa-graduation-cap</iconCls> 
     <cls>mainNav</cls> 
    </element> 
    <element> 
     <leaf>false</leaf> 
     <text>Institution</text> 
     <iconCls>x-fa fa-university</iconCls> 
     <cls>mainNav</cls> 
     </element> 
    </children> 
</root> 
+0

現在你叫什麼結果呢?也許我可以看到問題來源。但爲什麼你要在循環之前添加標籤?結果爲第一行「」。 – fairtrax

+0

你應該使用一個庫,像:https://stackoverflow.com/a/5059411/3959856 –

+0

對不起它我的錯誤我刪除@fairtrax其被錯誤在該代碼添加的元素部分 –

回答

0

您用於創建X的方式ML文件是錯誤的,因爲你硬編碼你的代碼中的每一件事。假設你將來想改變你的XML文件。這將花費太多時間,對於您來說,更改XML也將非常繁瑣。因此,請按照以下文檔鏈接創建XML文件,它將幫助您生成XML文件。

https://www.mkyong.com/java/how-to-create-xml-file-in-java-dom/

如果您有任何關於執行任何查詢,請讓我知道。

+0

確定我知道我會改變這樣我的代碼,但我不知道該如何實現那種被上面的XML文件中給出 –

+0

仔細閱讀文檔的XML格式。你必須實現的一件事是你的resultSet輸出的「for循環」。並在文檔中寫入文檔的代碼。 –

+0

這是真正的問題,我不知道如何讓這樣的循環,這讓我期望的輸出@ChetanTayade –

相關問題