Java SQL ResultSet toXML(ResultSet rs)

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

Description

Create xml string - fastest, but may have encoding issues

License

Open Source License

Parameter

Parameter Description
rs - a Result set

Exception

Parameter Description
SQLException - If error when read data from database

Return

- XML string of a result set

Declaration

public static String toXML(ResultSet rs) throws SQLException 

Method Source Code

//package com.java2s;
/*/*from w  w  w  . ja va 2 s . com*/
 * Copyright (C) 2010 Viettel Telecom. All rights reserved.
 * VIETTEL PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

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

public class Main {
    /**
     * Create xml string - fastest, but may have encoding issues
     *
     * @param rs - a Result set
     * @return - XML string of a result set
     * @throws SQLException - If error when read data from database
     */
    public static String toXML(ResultSet rs) throws SQLException {
        ResultSetMetaData rsmd = rs.getMetaData();
        int colCount = rsmd.getColumnCount();
        StringBuilder xml = new StringBuilder();
        xml.append("<Results>");

        while (rs.next()) {
            xml.append("<Row>");

            for (int i = 1; i <= colCount; i++) {
                String columnName = rsmd.getColumnName(i);
                Object value = rs.getObject(i);
                xml.append("<").append(columnName).append(">");

                if (value != null) {
                    xml.append(value.toString().trim());
                }
                xml.append("</").append(columnName).append(">");
            }
            xml.append("</Row>");
        }

        xml.append("</Results>");

        return xml.toString();
    }
}

Related

  1. moveToPageNo(ResultSet rs, int pageNo, int pageSize)
  2. parseSqlResultToListMap(ResultSet rs)
  3. putResultSetToMap(final Hashtable map, final ResultSet resultSet, final String... keys)
  4. safeGetString(ResultSet r, String name)
  5. saveResultsetAsFile(ResultSet rs, String filePath, String fileName)
  6. writeResultSetToFile(String path, ResultSet results)