0

I want to create an XML file by same like given below after getting data from db like this:my db table looks like this any help would be appreciated as I am newbie in XML file creation by java Servlet.

My XML file which i want to be created by a servlet:

   <?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 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>
  • what's your result now? Maybe then I can see the problem source. But why do yo add tag before the loop? That results to "" at the first row. – fairtrax Sep 14 '17 at 07:13
  • you should use a library for that like: https://stackoverflow.com/a/5059411/3959856 – Jack Flamp Sep 14 '17 at 07:14
  • sorry its my mistake i remove the element part which is added by mistake in this code @fairtrax – Haseeb Liaqat Sep 14 '17 at 07:33
  • @JackFlamp u are right but i don't want to parse my already created XML file from that XML code was only the demo to showcase which type of data i want to create with java servlet – Haseeb Liaqat Sep 14 '17 at 07:36
  • @Haaseb Liaqat so what is wrong with your output xml? How does it look like? – fairtrax Sep 14 '17 at 07:39
  • @fairtrax i have edit my question u can know see my currently generated XML file which of course not the result which i want to achieve – Haseeb Liaqat Sep 14 '17 at 07:47
  • I do not understand the binding between the excel rows. In your desired xml you have the Setup row as a parent of the others. Why? How is the relationship indicated? – fairtrax Sep 14 '17 at 14:01
  • yes setup is the parents of other because i use this XML in my navigation bar in Sencha Extjs so setup is the main parent which has 2 child Academics and Institution other elements are subchilds of these parents – Haseeb Liaqat Sep 15 '17 at 05:51
  • Do you have a formal schema for your XML by any chance? – Steve C Sep 15 '17 at 09:45

1 Answers1

0

The way you are using for creation of XML file is wrong because you hardcode every thing in your code. Suppose you want to change your XML file in future. It will take too much time it will also very tedious for you to change your XML. So follow below document link for creating XML file it will help you to generate a XML File.

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

If you have any query regarding implementation please let me know.

  • ok i understand i will change my code like this but i dont know how to achieve that kind of XML format which is the given above in XML file – Haseeb Liaqat Sep 14 '17 at 07:39
  • read the documentation carefully. The one thing that you have to implement is "for loop" for your resultSet output. and write the documentation's code inside thatloop. – Chetan Tayade Sep 14 '17 at 09:16
  • That's the real problem i don't know how to make such for loop which gets me my desired output @ChetanTayade – Haseeb Liaqat Sep 14 '17 at 09:32