简体   繁体   English

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

[英]how to create XML file after retriving data from mysql DB?

I want to create an XML file by same like given below after getting data from db like this: 我想从数据库中获取数据后,像下面一样创建一个XML文件,如下所示: 我的数据库表看起来像这样 any help would be appreciated as I am newbie in XML file creation by java Servlet. 任何帮助将不胜感激,因为我是Java Servlet创建XML文件的新手。

My XML file which i want to be created by a servlet: 我想由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>

So far I have tried this which is not generated the result that I want please help me out: 到目前为止,我已经尝试过了这并没有产生我想要的结果,请帮帮我:

   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();


}

currently my generated xml file looks like this: 目前,我生成的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>

The way you are using for creation of XML file is wrong because you hardcode every thing in your code. 用于创建XML文件的方式是错误的,因为您对代码中的所有内容进行了硬编码。 Suppose you want to change your XML file in future. 假设您以后想更改XML文件。 It will take too much time it will also very tedious for you to change your XML. 更改XML会花费太多时间,也非常繁琐。 So follow below document link for creating XML file it will help you to generate a XML File. 因此,请按照下面的文档链接创建XML文件,它将帮助您生成XML文件。

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

If you have any query regarding implementation please let me know. 如果您对实施有任何疑问,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM