我想創建由同樣喜歡從數據庫這樣獲取數據後,下面給出一個XML文件:我用的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>
現在你叫什麼結果呢?也許我可以看到問題來源。但爲什麼你要在循環之前添加標籤?結果爲第一行「」。 –
fairtrax
你應該使用一個庫,像:https://stackoverflow.com/a/5059411/3959856 –
對不起它我的錯誤我刪除@fairtrax其被錯誤在該代碼添加的元素部分 –