我有以下代码将xml文件转换为html文件。这是由许多线程访问的。转换方法每次只附加内容。这是第一个线程内容保留在所有后续线程中html文件。
public class CreateHTML
{
TransformerFactory tFactory;
Source xslDoc;
Source xmlDoc;
OutputStream htmlFile;
public CreateHTML(String xslDocFileName,String xmlDocFileName,String outputFileName) throws FileNotFoundException
{
xslDoc=new StreamSource(xslDocFileName);
xmlDoc=new StreamSource(xmlDocFileName);
htmlFile=new FileOutputStream(outputFileName);
}
public synchronized void createOutputFile() throws Exception
{
try
{
tFactory=TransformerFactory.newInstance();
tFactory.setAttribute("indent-number",new Integer(2));
Transformer transformer = tFactory.newTransformer(xslDoc);
transformer.setOutputProperty(OutputKeys.INDENT,"yes");
transformer.transform(xmlDoc, new StreamResult(htmlFile));
}
catch (TransformerException ex)
{
Logger.getLogger(CreateHTML.class.getName()).log(Level.SEVERE, null, ex);
throw ex;
}
}
}