Hi,
The code below prints :
<Results><Row>< customerName>je ff</customerName><a ddress>None</address><teleph one>None</telephone><birt hday>2007-01-12 13:58:20.0</birthday></Row></Results>
now the problem is i want to make it return the columns with its appropriate datatypes.. i.e. :
<Results><Row>< customerName datatype = "varchar">j eff</customerName><a ddress datatype = "varchar">N one</address><teleph one datatype = "varchar">N one</telephone><birt hday datatype = "date">2007-01-12 13:58:20.0</birthday></Row></Results>
Here's the code :
Thanks in advance. ^^
Regards,
Jeff
The code below prints :
<Results><Row>< customerName>je ff</customerName><a ddress>None</address><teleph one>None</telephone><birt hday>2007-01-12 13:58:20.0</birthday></Row></Results>
now the problem is i want to make it return the columns with its appropriate datatypes.. i.e. :
<Results><Row>< customerName datatype = "varchar">j eff</customerName><a ddress datatype = "varchar">N one</address><teleph one datatype = "varchar">N one</telephone><birt hday datatype = "date">2007-01-12 13:58:20.0</birthday></Row></Results>
Here's the code :
Code:
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class JDBCUtil
{
/***********| PRINT FORMAT # 1 - CREATE XML STRING |*************/
public static String toXML(ResultSet rs) throws SQLException
{
ResultSetMetaData rsmd = rs.getMetaData();
int colCount = rsmd.getColumnCount();
StringBuffer xml = new StringBuffer();
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("'<" + columnName + ">");
if (value != null)
{
xml.append("'" + value.toString().trim() + "'");
}
xml.append("</" + columnName + ">");
}
xml.append("</Row>");
}
xml.append("</Results>");
return xml.toString();
}
}
Regards,
Jeff
Comment