public class XMLTest
{
...
private void resultSetToXML(String stylesheet)
{
SqlXMLReader sxreader = new SqlXMLReader();
SqlInputSource sis = new SqlInputSource(rs);
sis.setEncoding("gb2312");
SAXSource source = new SAXSource(sxreader, sis);
StreamResult result = new StreamResult(new File("Test.xml"));
try
{
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t;
if (stylesheet == null)
{
t = tf.newTransformer();
}
else
{
InputStream xslstream = new FileInputStream(new File(stylesheet));
StreamSource xslsource = new StreamSource(xslstream);
t = tf.newTransformer(xslsource);
}
t.transform(source, result);
}
catch(Exception e)
{
e.printStackTrace();
}
}
...
}
public class SqlXMLReader implements XMLReader
{
protected ContentHandler _handler;
...
public void parse(InputSource is) throws IOException, SAXException
{
try
{
...
if (_handler == null)
{
throw new SAXException("No XML ContentHandler");
}
....
// Send startDocument and startElement events to handler
_handler.startDocument();
_handler.endDocument();
} catch (SQLException ex)
{
throw new SAXException(ex);
} catch (ClassCastException cce)
{
throw new SAXException("Parse requires SqlInputSource");
}
}
...
}
为什么生成的总是<?xml version="1.0" encoding="UTF-8"?>,如何做才能作为<?xml version="1.0" encoding="GB2312"?>啊?