SqlXMLReader.java

warning: realpath(): open_basedir restriction in effect. File(/tmp) is not within the allowed path(s): (/var/www/aliabdelaziz.org/data:.) in /var/www/aliabdelaziz.org/data/www/aliabdelaziz.org/includes/file.inc on line 287.

/*
* SqlXMLReader.java
*
* Created on April 29, 2007, 12:07 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package com.arpu.opencds.datatransformation;

import java.io.*;
import java.net.URL;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

/** A utility class that parses a Result Set Value
* and outputs its contents using SAX2 events.
* For simple values, the SQL Result Set file may look like this:
*

 * Country_ID	Country_Name
 *     1	Egypt
 *     2	Algeria
 * 

* Column Names are used as the Tag Names fro the constructed XML:
*

 * 
­­ * <countries>
 *  <row>
 *      <country_id>1</country_id>
 *      <country_name>Egypt</country_name>
 *  </row>
 *  <row>
 *      <country_id>2</country_id>
 *      <country_name>Algeria</country_name>
 *  </row>
 * </countries>
 * 

* And double quotes are used when the values contain quotes. This parser
* is smart enough to trim spaces around commas, as well.
*
* @author Ali Abdel-Aziz
* http://www.aliabdelaziz.com/
*/
public class SqlXMLReader extends AbstractXMLReader {

private static final Attributes EMPTY_ATTR = new AttributesImpl();

/**
* public class SqlXMLReader extends AbstractXMLReader {
*
* // an empty attribute for use with SAX
* private static final Attributes EMPTY_ATTR = new AttributesImpl( );
*
* /**
* Parse SQL ResultSet. SAX events are delivered to the ContentHandler
* that was registered via setContentHandler.
*
* @param input the result set value to parse.
*/
public void parse(InputSource input) throws IOException,
SAXException {
// Get result set from SqlInputSource.
SqlInputSource sis = (SqlInputSource)input;
ResultSet rs = sis.getResultSet();

ContentHandler _handler = getContentHandler( );
if (_handler == null) {
throw new SAXException("No XML ContentHandler");
}

// Get information about result set
ResultSetMetaData md;
int nColumns;
try {
md = rs.getMetaData();
nColumns = md.getColumnCount();

int iRow = 0;
Attributes attrs = null;

String uri = "", docroot = "Countries";
// Send startDocument and startElement events
// to handler
_handler.startDocument();
_handler.startElement(uri, docroot, docroot, EMPTY_ATTR);
while (rs.next()) {
// Output "row" tag
_handler.startElement(uri, "ROW", "ROW", EMPTY_ATTR);
//outputIgnorableWhitespace("\n");
System.out.println();

String s;
for (int i = 1; i <= nColumns; i++) {
String tag = md.getColumnName(i);
_handler.startElement(uri, tag, tag, EMPTY_ATTR);
s = rs.getString(i);
if (s == null) {
s = "";
}

_handler.characters(s.toCharArray(), 0, s.length());
//outputString(s, _handler);
System.out.print(s);
_handler.endElement(uri, tag, tag);
//outputIgnorableWhitespace("\n");
System.out.print(s);
}
_handler.endElement(uri, "ROW", "ROW");
//outputIgnorableWhitespace("\n");
System.out.println();
}

_handler.endElement(uri, docroot, docroot);
//outputIgnorableWhitespace("\n");
System.out.println();
_handler.endDocument();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}

Nice work. Following this

Nice work. Following this coding surely creates good result. - Stephen Samuelian

Powered by Drupal - Design by artinet