title: how to create XML file after retriving data from mysql DB?
body: <p>I want to create an XML file by same like given below after getting data from db like this:<a href="https://i.stack.imgur.com/gfxli.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gfxli.png" alt="my db table looks like this"></a> any help would be appreciated as I am newbie in XML file creation by java Servlet.</p>
  
  <p>My XML file which i want to be created by a servlet:</p>
  
  <pre><code>   &lt;?xml version="1.0" encoding="UTF-8"?&gt;
        &lt;root&gt;
         &lt;expanded&gt;true&lt;/expanded&gt;
       &lt;children&gt;
       &lt;element&gt;
          &lt;text&gt;Setup&lt;/text&gt;
          &lt;leaf&gt;false&lt;/leaf&gt;
          &lt;iconCls&gt;x-fa fa-gears&lt;/iconCls&gt;
          &lt;cls&gt;mainNav&lt;/cls&gt;
          &lt;children&gt;
              &lt;element&gt;
                  &lt;leaf&gt;false&lt;/leaf&gt;
                  &lt;text&gt;Academics&lt;/text&gt;
                  &lt;iconCls&gt;x-fa fa-graduation-cap&lt;/iconCls&gt;
                  &lt;cls&gt;mainNav&lt;/cls&gt;
                  &lt;children&gt;
              &lt;element&gt;
                  &lt;leaf&gt;true&lt;/leaf&gt;
                  &lt;text&gt;Session&lt;/text&gt;
                  &lt;iconCls&gt;x-fa fa-star&lt;/iconCls&gt;
                  &lt;cls&gt;PIU.view.setup.academics.AcademicSession&lt;/cls&gt;
              &lt;/element&gt;
              &lt;element&gt;
                  &lt;leaf&gt;true&lt;/leaf&gt;
                  &lt;text&gt;Faculty&lt;/text&gt;
                  &lt;iconCls&gt;x-fa fa-star-o&lt;/iconCls&gt;
                  &lt;cls&gt;PIU.view.setup.academics.AcademicFaculty&lt;/cls&gt;
              &lt;/element&gt;
              &lt;element&gt;
                  &lt;leaf&gt;true&lt;/leaf&gt;
                  &lt;text&gt;Shifts&lt;/text&gt;
                  &lt;iconCls&gt;x-fa fa-star-o&lt;/iconCls&gt;
                  &lt;cls&gt;PIU.view.setup.academics.MaintainShifts&lt;/cls&gt;
              &lt;/element&gt;
             &lt;/children&gt;
              &lt;/element&gt;
              &lt;element&gt;
                  &lt;leaf&gt;false&lt;/leaf&gt;
                  &lt;text&gt;Institution&lt;/text&gt;
                  &lt;iconCls&gt;x-fa fa-university&lt;/iconCls&gt;
                  &lt;cls&gt;mainNav&lt;/cls&gt;
              &lt;children&gt;
              &lt;element&gt;
                  &lt;leaf&gt;true&lt;/leaf&gt;
                  &lt;text&gt;Institution&lt;/text&gt;
                  &lt;iconCls&gt;x-fa fa-globe&lt;/iconCls&gt;
                  &lt;cls&gt;PIU.view.setup.institution.DefineInstitution&lt;/cls&gt;
              &lt;/element&gt;
              &lt;element&gt;
                  &lt;leaf&gt;true&lt;/leaf&gt;
                  &lt;text&gt;Facilities&lt;/text&gt;
                  &lt;iconCls&gt;x-fa fa-building&lt;/iconCls&gt;
                  &lt;cls&gt;PIU.view.setup.institution.MaintainFacilities&lt;/cls&gt;
              &lt;/element&gt;
              &lt;element&gt;
                  &lt;leaf&gt;true&lt;/leaf&gt;
                  &lt;text&gt;Letter Head&lt;/text&gt;
                  &lt;iconCls&gt;x-fa fa-info&lt;/iconCls&gt;
              &lt;/element&gt;
            &lt;/children&gt;
                &lt;/element&gt;
             &lt;/children&gt;
            &lt;/element&gt;
          &lt;/children&gt; 
       &lt;/root&gt;
  </code></pre>
  
  <p>So far I have tried this which is not generated the result that I want please help me out:</p>
  
  <pre><code>   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&lt;String&gt; rows = new ArrayList&lt;String&gt;();
      rows.add("&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;");
      rows.add("&lt;root&gt;"); 
      rows.add("&lt;expanded&gt;true&lt;/expanded&gt;");
      rows.add("&lt;children&gt;");
      while(rs.next()) 
      { 
          rows.add("&lt;element&gt;");
          rows.add("&lt;leaf&gt;");
          rows.add(rs.getString(4));
          rows.add("&lt;/leaf&gt;");
          rows.add("&lt;text&gt;");
          rows.add(rs.getString(1));
          rows.add("&lt;/text&gt;");
          rows.add("&lt;iconCls&gt;");
          rows.add(rs.getString(3));
          rows.add("&lt;/iconCls&gt;");
          rows.add("&lt;cls&gt;");
          rows.add(rs.getString(2));
          rows.add("&lt;/cls&gt;");
          rows.add("&lt;/element&gt;");
  
      }
      rows.add("&lt;/children&gt;");
      rows.add("&lt;/root&gt;"); 
  
      Iterator&lt;String&gt; 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();
  
  
  }
  </code></pre>
  
  <p>currently my generated xml file looks like this:</p>
  
  <pre><code>  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;root&gt;
   &lt;expanded&gt;true&lt;/expanded&gt;
    &lt;children&gt;
      &lt;element&gt;
          &lt;leaf&gt;true&lt;/leaf&gt;
          &lt;text&gt;Institution&lt;/text&gt;
          &lt;iconCls&gt;x-fa fa-globe&lt;/iconCls&gt;
          &lt;cls&gt;PIU.view.setup.institution.DefineInstitution&lt;/cls&gt;
      &lt;/element&gt;
      &lt;element&gt;
          &lt;leaf&gt;true&lt;/leaf&gt;
          &lt;text&gt;Facilities&lt;/text&gt;
          &lt;iconCls&gt;x-fa fa-building&lt;/iconCls&gt;
          &lt;cls&gt;PIU.view.setup.institution.MaintainFacilities&lt;/cls&gt;
      &lt;/element&gt;
      &lt;element&gt;
          &lt;leaf&gt;true&lt;/leaf&gt;
          &lt;text&gt;Session&lt;/text&gt;
          &lt;iconCls&gt;x-fa fa-star&lt;/iconCls&gt;
          &lt;cls&gt;PIU.view.setup.academics.AcademicSession&lt;/cls&gt;
      &lt;/element&gt;
      &lt;element&gt;
          &lt;leaf&gt;true&lt;/leaf&gt;
          &lt;text&gt;Faculty&lt;/text&gt;
          &lt;iconCls&gt;x-fa fa-star-o&lt;/iconCls&gt;
          &lt;cls&gt;PIU.view.setup.academics.AcademicFaculty&lt;/cls&gt;
      &lt;/element&gt;
      &lt;element&gt;
          &lt;leaf&gt;true&lt;/leaf&gt;
          &lt;text&gt;Shifts&lt;/text&gt;
          &lt;iconCls&gt;x-fa fa-star-o&lt;/iconCls&gt;
          &lt;cls&gt;PIU.view.setup.academics.MaintainShifts&lt;/cls&gt;
      &lt;/element&gt;
      &lt;element&gt;
          &lt;leaf&gt;false&lt;/leaf&gt;
          &lt;text&gt;Setup&lt;/text&gt;
          &lt;iconCls&gt;x-fa fa-gears&lt;/iconCls&gt;
          &lt;cls&gt;mainNav&lt;/cls&gt;
      &lt;/element&gt;
      &lt;element&gt;
          &lt;leaf&gt;false&lt;/leaf&gt;
          &lt;text&gt;Academics&lt;/text&gt;
          &lt;iconCls&gt;x-fa fa-graduation-cap&lt;/iconCls&gt;
          &lt;cls&gt;mainNav&lt;/cls&gt;
      &lt;/element&gt;
      &lt;element&gt;
          &lt;leaf&gt;false&lt;/leaf&gt;
          &lt;text&gt;Institution&lt;/text&gt;
          &lt;iconCls&gt;x-fa fa-university&lt;/iconCls&gt;
          &lt;cls&gt;mainNav&lt;/cls&gt;
        &lt;/element&gt;
     &lt;/children&gt;
   &lt;/root&gt;
  </code></pre>