gpt4 book ai didi

java - 从 mysql DB 检索数据后如何创建 XML 文件?

转载 作者:行者123 更新时间:2023-11-29 18:25:46 25 4
gpt4 key购买 nike

我想在从数据库获取数据后创建一个与下面给出的相同的XML文件,如下所示:my db table looks like this任何帮助将不胜感激,因为我是 java Servlet 创建 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>

最佳答案

您用于创建 XML 文件的方式是错误的,因为您对代码中的所有内容进行了硬编码。假设您将来想要更改 XML 文件。更改 XML 会花费太多时间,而且非常乏味。因此,请按照下面的文档链接创建 XML 文件,它将帮助您生成 XML 文件。

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

如果您对实现有任何疑问,请告诉我。

关于java - 从 mysql DB 检索数据后如何创建 XML 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46212638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com