Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Reading Empty Value from SQL Server 2000 & writing to the XML

  Asked By: Joel    Date: Oct 20    Category: Java    Views: 1103
  


public static StringBuffer toXML(ResultSet rs) throws SQLException { ResultSetMetaData rsmd = rs.getMetaData(); int colCount = rsmd.getColumnCount(); StringBuffer xml = new StringBuffer(); xml.append("<?xml version =\"1.0\"?>"); // xml.append("<!DOCTYPE XML SYSTEM \"Holiday.dtd\">"); //
System.out.print(xml);*/ xml.append("<XML>"); while (rs.next()) { xml.append("<Row>"); // rsmd.get for (int i = 1; i <= colCount; i++) { String columnName = rsmd.getColumnName(i); Object value = rs.getObject(i); //StringBuffer value = rs.getObject(i); xml.append("<" + columnName + ">"); // javax.swing.JOptionPane.showMessageDialog(null,columnName+"-"+value+"-" ); // String chk = value.toString().trim(); if (value != null) { xml.append(value.toString().trim()); // xml.append(" "); } else { // javax.swing.JOptionPane.showMessageDialog(null,"Return NUll" ); xml.append("NULL"); } xml.append("</"+columnName+">"); } xml.append("</Row>"); } xml.append("</XML>"); // javax.swing.JOptionPane.showMessageDialog(null,xml.toString()); // String s = rsmd.getTableName(0); // System.out.println("Table name "+s); //System.out.println(xml); return xml;//.toString(); }



this code gives XML o/p
if there is empty (Not Null,not " " ) so it not understand by the xml parser gives Exception

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Troy Kelley     Answered On: Oct 20

In following link there's some utilities which can handle null, or the others ,
http://jakarta.apache.org/commons/lang/apidocs/index.html
for example:


public static Object defaultIfNull(Object object,
Object defaultValue)
Returns a default value if the object passed is null.
ObjectUtils.defaultIfNull(null, null) = null
ObjectUtils.defaultIfNull(null, "") = ""
ObjectUtils.defaultIfNull(null, "zz") = "zz"
ObjectUtils.defaultIfNull("abc", *) = "abc"

ObjectUtils.defaultIfNull(Boolean.TRUE, *) = Boolean.TRUE
Parameters:
object - the Object to test, may be null
defaultValue - the default value to return, may be null
Returns:
object if it is not null, defaultValue otherwise
or :

public static boolean isEmpty(String str)
Checks if a String is empty  ("") or null.
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false
NOTE: This method changed in Lang version 2.0. It no longer trims the String. That functionality is available in isBlank().
Parameters:
str - the String to check, may be null
Returns:
true if the String is empty or null
or :

public static boolean isBlank(String str)
Checks if a String is whitespace, empty ("") or null.
StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = false

Parameters:
str - the String to check, may be null
Returns:
true if the String is null, empty or whitespace
Since:
2.0

 
Answer #2    Answered By: Gorkem Yilmaz     Answered On: Oct 20

the simple solution is that you could check resultset before you append your node and if it's not null or " " then append it .

 




Tagged: