Java SQL ResultSet to resultSetToXML(ResultSet rs)

Here you can find the source of resultSetToXML(ResultSet rs)

Description

result Set To XML

License

Open Source License

Declaration

public static String resultSetToXML(ResultSet rs) throws SQLException 

Method Source Code


//package com.java2s;
/*/*from   w  w  w . j  a  v  a2 s . c  o  m*/
Dynamo Web Services is a web service project for administering LucidDB
Copyright (C) 2010 Dynamo Business Intelligence Corporation
    
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version approved by Dynamo Business Intelligence Corporation.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class Main {
    public static String resultSetToXML(ResultSet rs) throws SQLException {

        ResultSetMetaData rsmd = rs.getMetaData();
        int colCount = rsmd.getColumnCount();
        StringBuffer ret = new StringBuffer();

        while (rs.next()) {
            ret.append("<Table>");
            for (int i = 1; i <= colCount; i++) {
                String columnName = rsmd.getColumnName(i).replaceAll(" ", "&nbsp;");
                String value = rs.getString(i);
                if (value != null && (value.indexOf("<") != -1 || value.indexOf(">") != -1)) {
                    value = "<![CDATA[" + value + "]]>";
                }
                ret.append("<" + columnName + ">" + value);
                ret.append("</" + columnName + ">");
            }
            ret.append("</Table>");
        }
        return ret.toString();

    }
}

Related

  1. resultSetToMap(Map mappedValues, ResultSet rs)
  2. resultSetToOrderedMap(ResultSet rs)
  3. resultSetToSet(PreparedStatement statement)
  4. resultSetToString(ResultSet resultSet)
  5. resultSetToString(ResultSet resultSet)
  6. toArrayList(ResultSet rs)
  7. toAttributeMap(ResultSet resultSet)
  8. toJSONFromResultSet(ResultSet resultSet)
  9. toListMap(int limit, ResultSet rs)