Hello,
I have looked through the mailing list archives and I have seen
threads where people have had similar problems, but I haven't read
anything that jumped out at me as an answer to my problem.
I have a simple XMLReader that opens a connection to a data
base, and generates SAX events for the rows in a result set... I am
piping the events through a TransformerHandler but I am not getting what
I think I should get. I have a feeling it has something to do with my
namespace (or lack of one) but I am not sure how to go about fixing it
if that is the case. Here is the parse method from my XMLReader:
---------------------------------SNIP---------------------------------
public void parse(DbInputSource input) throws IOException, SAXException
{
//If we don't have a handler, we don't need to do anything.
if(docHandler == null)return;
Attributes EMPTY_ATTR = new AttributesImpl();
Connection con = input.getConnection();
String query = input.getQuery();
Statement stmt = null;
try{
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
int columns = rsmd.getColumnCount();
docHandler.startDocument();
docHandler.startElement("","table","table",EMPTY_ATTR);
while(rs.next()){
docHandler.startElement("","row","row",EMPTY_ATTR);
for(int i = 1; i <= columns; i++){
String colName = rsmd.getColumnName(i);
String value = rs.getString(i);
docHandler.startElement("",colName,colName,EMPTY_ATTR);
docHandler.characters(value.toCharArray(),0,value.length());
docHandler.endElement("",colName,colName);
}
docHandler.endElement("","row","row");
}
docHandler.endElement("","table","table");
docHandler.endDocument();
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
stmt.close();
}
catch(Exception e){}
try{
con.close();
}
catch(Exception e){}
}
}
---------------------------------SNIP---------------------------------
So I then use that parser in a simple test program like the following:
---------------------------------SNIP---------------------------------
try {
TransformerFactory tFactory = TransformerFactory.newInstance();
if(tFactory.getFeature(SAXSource.FEATURE) &&
tFactory.getFeature(SAXResult.FEATURE)){
SAXTransformerFactory saxTFactory =(SAXTransformerFactory)tFactory;
//TransformerHandler tHandler = saxTFactory.newTransformerHandler();
TransformerHandler tHandler = saxTFactory.newTransformerHandler(new
StreamSource("test2.xsl"));
XMLDBSaxParser parser = new XMLDBSaxParser();
parser.setContentHandler(tHandler);
//parser.setProperty("http://xml.org/sax/properties/lexical-handler",tHa
ndler);
Serializer serializer =
SerializerFactory.getSerializer(OutputPropertiesFactory.getDefaultMethod
Properties("xml"));
serializer.setOutputStream(System.out);
tHandler.setResult(new SAXResult(serializer.asContentHandler()));
// Parse the XML input document.
DbInputSource input = new DbInputSource();
input.setQuery("select * from foo.bar");
parser.parse(input);
}
}
catch (Exception e) {
e.printStackTrace();
}
---------------------------------SNIP---------------------------------
I am using a simple stylesheet right now for testing (test2.xsl). When I
do the following I get output:
<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
version = "1.0" >
<xsl:output method = "xml" />
<xsl:template match = "/" >
<Test>
<xsl:apply-templates select = "//row" />
</Test>
</xsl:template>
<xsl:template match = "row" >
<xsl:value-of select="name()" />
</xsl:template>
</xsl:stylesheet>
I end up with <Test>rowrowrowrowrowrowrow...</Test>, which is what it
should do. However, when I add start and end tags (<test1> and </test1>)
in the template for "row" no output is produced at all:
<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
version = "1.0" >
<xsl:output method = "xml" />
<xsl:template match = "/" >
<Test>
<xsl:apply-templates select = "//row" />
</Test>
</xsl:template>
<xsl:template match = "row" >
<test1>
<xsl:value-of select="name()" />
</test1>
</xsl:template>
</xsl:stylesheet>
I have a feeling I am not generating the events correctly. Can anyone
give me an idea as to what I am doing wrong? I have found examples that
do the same thing I am doing above, but they are 1 or 2 years old so I
figure something may have changed and I am not finding it. I appreciate
your help!
-Dan Feather