private static void process(String xmlStream, String absoluteXSLFile, Writer resultWriter, String outputEnc, boolean debug, boolean comp_xsl)
throws cwException
{
try {
Templates tplate = (Templates)getHashValue(absoluteXSLFile);
Transformer tformer = null;
if (tplate == null) {
if (absoluteXSLFile == null || absoluteXSLFile.length() == 0) {
throw new cwException("Invalid stylesheet");
}
File fXsl = new File(absoluteXSLFile);
if (!fXsl.exists()) {
String error = "Style sheet not found: ";
if (debug) {
error += fXsl.getAbsolutePath();
}
throw new cwException(error);
}
if (comp_xsl) {
tplate = loadCompiledXSL(fXsl);
tformer = tplate.newTransformer();
} else {
TransformerFactory tFactory = TransformerFactory.newInstance();
tformer = tFactory.newTransformer(new StreamSource(absoluteXSLFile));
}
} else {
tformer = tplate.newTransformer();
}
StringReader xmlR = new StringReader(xmlStream);
StringWriter outW = new StringWriter();
//在运行到此处,将string类型 解析成xml出错.!
tformer.transform(new StreamSource(xmlR), new StreamResult(outW));
xmlR.close();
outW.close();
resultWriter.write(outW.toString());
resultWriter.flush();
} catch (TransformerConfigurationException e) {
throw new cwException(e.getMessage());
} catch (TransformerException e) {
throw new cwException(e.getMessage());
} catch (IOException e) {
throw new cwException(e.getMessage());
}
}
javax.xml.transform.TransformerException: java.lang.NullPointerException
网上有一种说法是在程序处理上,没有对于空字符做出判断导致.
而我这次遇到了这种情况,最终查出原因是在生成xml之后写入到页面上去,系统配置文件开启了缓存,会去加载*.xsl文件到缓存中.在生成xml后写入缓存中的.xsl文件
但是服务器上配置文件中没有设置缓存.xsl文件(文件内容为空),却又在系统中开启了缓存.导致系统在xml生成之后去找缓存中.xsl文件时找不到报错.