Here you can find the source of toXML(ResultSet rs)
Parameter | Description |
---|---|
rs | - a Result set |
Parameter | Description |
---|---|
SQLException | - If error when read data from database |
public static String toXML(ResultSet rs) throws SQLException
//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(); } }