0

我有以下代码将xml文件转换为html文件。这被许多线程访问。transform方法只是每次都附加内容。即第一个线程的内容保留在所有后续线程的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;
        }
    }
}
4

0 回答 0