title: Creating XML file from Database?
body: <p>I have a database and there are multiple tables in db. Now I want to generate the XML file of a particular table or all the tables at on shot.
  I tried lots of stuff available on net, but not able to do what i was trying.
  Suppose I have a table like:</p>
  
  <pre><code> id    created  end_num    gateway   type   modified  name    start_num  sub_type
   1       NULL    30      172.20.1.2   SIP     NULL   Bangalore    20       IP   
  
   2       NULL    15      172.20.2.5   SIP     NULL   Delhi        15       UDP
  </code></pre>
  
  <p>Now I want to generate of following XML File:</p>
  
  <pre><code> &lt;XYZ&gt;
     &lt;users&gt;
       &lt;user name="Bangalore" type="SIP"&gt;
       &lt;sub_type&gt;IP&lt;/sub_type&gt;
       &lt;start_num&gt;20&lt;/start_num&gt;
       &lt;end_num&gt;30&lt;/end_num&gt;
       &lt;gateway&gt;172.20.1.2&lt;/gateway&gt;
       &lt;/user&gt;
  
       &lt;user name="Delhi" type="SIP"&gt;
       &lt;sub_type&gt;UDP&lt;/sub_type&gt;
       &lt;start_num&gt;15&lt;/start_num&gt;
       &lt;end_num&gt;15&lt;/end_num&gt;
       &lt;gateway&gt;172.20.2.5&lt;/gateway&gt;
       &lt;/user&gt;
  
     &lt;/users&gt;
   &lt;/XYZ&gt;   
  </code></pre>
  
  <p>Note: I need to add the tags like &lt; XYZ > and &lt; Users > from my side.</p>
  
  <p>I was trying to do something like below:</p>
  
  <pre><code>   @Transactional(readOnly=true)
     public void getCommit() throws SQLException, ParserConfigurationException 
    {
       System.out.println("INSIDE COMMIT REPO");
       // TODO Auto-generated method stub
  
       //String query = "FROM trunk";
       SessionImpl sessionImpl = (SessionImpl)sessionFactory.getCurrentSession();
       Connection connection = sessionImpl.connection();
       PreparedStatement preparedStatement = connection.prepareStatement("Select * FROM trunk");
       ResultSet rs = preparedStatement.executeQuery();
       ResultSetMetaData rsmd = rs.getMetaData();
       int columnsNumber = rsmd.getColumnCount();
       while (rs.next()) 
       {
          for (int i = 1; i &lt;= columnsNumber; i++) {
          if (i &gt; 1) System.out.print(",  ");
          String columnValue = rs.getString(i);
          System.out.print(columnValue + "  -----------  " +      rsmd.getColumnName(i));
        }
        System.out.println(" ");
      }
      toDoucumet(rs);
   }
  
   private Document toDoucumet(ResultSet rs) throws ParserConfigurationException, SQLException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.newDocument();
    Element results = doc.createElement("Users");
    doc.appendChild(results);
    ResultSetMetaData rsmd = rs.getMetaData(); **// Getting Error at this line  IllegalStateException**
    int colCount  = rsmd.getColumnCount();
    System.out.println(colCount);
    while(rs.next()){
    Element row = doc.createElement("user");
    results.appendChild(row);
    for (int i = 1; i &lt;= colCount; i++) 
    {
      System.out.println(rsmd.getColumnName(i));
      String columnName = rsmd.getColumnName(i);
      Object value = rs.getObject(i);
  
      Element node = doc.createElement(columnName);
      node.appendChild(doc.createTextNode(value.toString()));
      row.appendChild(node);
     }
  }
  return doc;
  </code></pre>
  
  <p>}</p>
  
  <p>Please provide me any other way to do so that after retrieving data from DB I can able to add root tags from my side.
  Thanks In Advance....... </p>