[jdom-interest] Seeking contributions ... db queries into jdom elements

Andres March Andres at eHealthContracts.com
Thu Nov 7 13:41:35 PST 2002


You can submit this to the commit list if you like.

import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import java.util.*;
import java.sql.*;
import java.io.OutputStream;

public class XMLUtil
{
    public final String ROW_ELEMENT_NAME      = "ROW";
    public final String ROOT_ELEMENT_NAME     = "ROWSET";

  public XMLUtil(){};

  public Document generate(ResultSet sqlResult)
  {
    
    try
    {
      if( sqlResult == null )
        return null;

      Element rootElem = generateManyRows( sqlResult );

      Document xmlDoc = new Document(rootElem);
     
      return xmlDoc;
    }
    catch(Exception e)
    {
    
      throw new RuntimeException(e.getMessage());
    }
    finally
    {
     
    }

  }

  public Element generateManyRows(ResultSet sqlResult) throws SQLException {
    return generateManyRows( sqlResult, ROOT_ELEMENT_NAME, ROW_ELEMENT_NAME );
  }

  public Element generateManyRows( ResultSet sqlResult, String rowsetName, String rowName ) throws SQLException {
    ResultSetMetaData md = sqlResult.getMetaData();
    sqlResult.beforeFirst();  // required to make sure you are at the first row

    Element rootElem = new Element( rowsetName );
    int rowCnt = 1;
    while(sqlResult.next())
    {
      Element rowElem = generateOneRow( sqlResult, rowName, rowCnt, md );

      rootElem.addContent(rowElem);
      rowCnt++;
    }
    return rootElem;
  }

  public Element generateOneRow(ResultSet sqlResult) throws SQLException {
    return generateOneRow( sqlResult, ROW_ELEMENT_NAME );
  }

  public Element generateOneRow(ResultSet sqlResult,String rowName) throws SQLException {
    ResultSetMetaData md = sqlResult.getMetaData();

    return generateOneRow( sqlResult, rowName, -1, md );
  }

  public Element generateOneRow(ResultSet sqlResult,String rowName,int rowCnt,ResultSetMetaData md) throws SQLException {
    int ccnt = md.getColumnCount();
    Element rowElem = new Element( rowName );

    if (rowCnt > 0) // otherwise it needs to be ignored
      rowElem.setAttribute("num",(new Integer(rowCnt)).toString());

    for( int i = 1 ; i <= ccnt ; i ++ )
    {
      String colName = (String) md.getColumnName(i);
      String dbName = colName;
			char qch = '?';
			char undch = '_';
			char blankch = ' ';
      colName = colName.replace(qch, undch);
      colName = colName.replace(blankch, undch);
      Element colElem = new Element(colName);
     
      if( md.getColumnType(i) == java.sql.Types.CLOB)
      {
        if( sqlResult.getClob(md.getColumnName(i)) != null)
        {
          colElem.setText(sqlResult.getClob(md.getColumnName(i)).toString());
        }
        else
        {
          colElem.setText("");
        }
      }
      else if ( md.getColumnType(i) == java.sql.Types.NUMERIC)
      {
        if( sqlResult.getBigDecimal(md.getColumnName(i)) != null)
        {
          colElem.setText(sqlResult.getBigDecimal(md.getColumnName(i)).toString());
        }
        else
        {
          colElem.setText("");
        }
      }

      else
      {
        colElem.setText(sqlResult.getString(md.getColumnName(i)));
      }
      rowElem.addContent(colElem);
    }
    return rowElem;
  }

  public static void printXml(Document doc)
  {
          try{
                  OutputStream out = System.out;
                  XMLOutputter fmt = new XMLOutputter();
                  fmt.output(doc, out);
                  out.flush();
          }
          catch(Exception e)
          {
                  e.printStackTrace();
                  throw new RuntimeException(e.getMessage());

          }

  }
  public static void printXml(Element elem)
  {
    try
    {
      Document doc = new Document(elem);
      printXml(doc);
    }
    catch(Exception e)
    {
      throw new RuntimeException(e.getMessage());
    }
  }

  public static Element copyElement(Element elem)
  {
    return (Element)elem.clone();
  }


  }

-----Original Message-----
From: emmettwa at onebox.com [mailto:emmettwa at onebox.com]
Sent: Thursday, November 07, 2002 12:21 PM
To: jdom-interest at jdom.org
Subject: [jdom-interest] Seeking contributions ... db queries into jdom
elements


Hi,

I couldn't find the contributions area from jdom.org...

Anyway, has anyone ever contributed some code which
lets you take a ResultSet and convert it into a JDOM element?

Thanks,

em

-- 
Emmett McLean
emmettwa at onebox.com - email
(866) 841-9139 x1160 - voicemail/fax

_______________________________________________
To control your jdom-interest membership:
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com



More information about the jdom-interest mailing list