Getting "net.sf.saxon.trans.XPathException: java.io.IOException: Stream Closed" exception in my java code during xslt transformation

huangapple go评论75阅读模式
英文:

Getting "net.sf.saxon.trans.XPathException: java.io.IOException: Stream Closed" exception in my java code during xslt transformation

问题

我正在使用XSLT和Java来处理并将输入的XML文件转换为多个结果文档。但是在我的Java转换类中,在transformer.transform(input, output)处出现以下错误。

堆栈跟踪:

java.io.IOException: 流已关闭
net.sf.saxon.trans.XPathException: java.io.IOException: 流已关闭
	at net.sf.saxon.event.XMLEmitter.close(XMLEmitter.java:264)
	at net.sf.saxon.event.ProxyReceiver.close(ProxyReceiver.java:90)
	at net.sf.saxon.event.UncommittedSerializer.close(UncommittedSerializer.java:53)
	at net.sf.saxon.event.ImplicitResultChecker.close(ImplicitResultChecker.java:92)
	at net.sf.saxon.event.ProxyReceiver.close(ProxyReceiver.java:90)
	at net.sf.saxon.event.ComplexContentOutputter.close(ComplexContentOutputter.java:507)
	at net.sf.saxon.Controller.transformDocument(Controller.java:1848)
	at net.sf.saxon.Controller.transform(Controller.java:1621)

Java代码片段:

String outputDirectory = fileParentLocation + "\\resultFolder\\";
FileTools.createDirectory(new File(outputDirectory));

try {
TransformerFactory factory =
		TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl", getClass().getClassLoader());

InputStream xsltResourceAsStream =
		getClass().getClassLoader().getResourceAsStream("ConvertInputXmlFiles.xslt");
Source xslt = new StreamSource(xsltResourceAsStream);
Result output = new StreamResult(new File(outputDirectory + "test.xml"));
		
Transformer transformer = factory.newTransformer(xslt);
transformer.setParameter("versionName", versionName);        

  if (filesToBeTransformed != null && !filesToBeTransformed.isEmpty()) {
	for (File file : filesToBeTransformed) {
		Source input = new StreamSource(file);
		transformer.transform(input, output);
	}
  }
}

提前感谢您的帮助!

英文:

I am using xslt along with java to process and convert input xml files to produce multiple result documents.
But I am getting below error at transformer.transform(input, output) in my java transformation class.

Please help me in this.

Stack Trace:

java.io.IOException: Stream Closed
net.sf.saxon.trans.XPathException: java.io.IOException: Stream Closed
	at net.sf.saxon.event.XMLEmitter.close(XMLEmitter.java:264)
	at net.sf.saxon.event.ProxyReceiver.close(ProxyReceiver.java:90)
	at net.sf.saxon.event.UncommittedSerializer.close(UncommittedSerializer.java:53)
	at net.sf.saxon.event.ImplicitResultChecker.close(ImplicitResultChecker.java:92)
	at net.sf.saxon.event.ProxyReceiver.close(ProxyReceiver.java:90)
	at net.sf.saxon.event.ComplexContentOutputter.close(ComplexContentOutputter.java:507)
	at net.sf.saxon.Controller.transformDocument(Controller.java:1848)
	at net.sf.saxon.Controller.transform(Controller.java:1621)

Java code snippet:

String outputDirectory = fileParentLocation + "\\resultFolder\\";
FileTools.createDirectory(new File(outputDirectory));

try {
TransformerFactory factory =
		TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl", getClass().getClassLoader());

InputStream xsltResourceAsStream =
		getClass().getClassLoader().getResourceAsStream("ConvertInputXmlFiles.xslt");
Source xslt = new StreamSource(xsltResourceAsStream);
Result output = new StreamResult(new File(outputDirectory + "test.xml"));
		
Transformer transformer = factory.newTransformer(xslt);
transformer.setParameter("versionName", versionName);        

  if (filesToBeTransformed != null && !filesToBeTransformed.isEmpty()) {
	for (File file : filesToBeTransformed) {
		Source input = new StreamSource(file);
		transformer.transform(input, output);
	}
  }
}

Thanks in advance !!

答案1

得分: 0

你正在尝试将多个转换结果附加到同一输出流,而那样做是行不通的。

我认为如果您创建一个FileOutputStream并将其提供给StreamResult,而不是提供File,那么这可能会起作用。基本上,谁创建流就负责关闭它,如果您提供了一个File,那么Saxon会创建流,并会在转换结束时关闭它,但是如果您创建流,则可以选择是否关闭它。

您的陈述中提到您要生成多个输出文档,但您的代码似乎试图生成单个结果文档。到底是哪种情况?

另请注意,对于Saxon,我们建议使用TransformerFactory.newTemplates()编译样式表一次,然后使用Templates.newTransformer()为每个转换创建新的Transformer。允许串行重用单个Transformer,但可能会导致内部缓存中资源的不必要累积。

英文:

You're trying to append multiple transformation results to the same output stream, and that doesn't work.

I think it would probably work if you created a FileOutputStream and supplied that in the StreamResult, instead of supplying the File. Basically, whoever creates a stream is responsible for closing it, and if you supply a File, then Saxon creates the stream, and will close it at the end of the transformation, but if you create the stream, then you can choose whether to close it or not.

Your prose question says you're trying to produce multiple output documents, but your code seems to be trying to produce a single result document. Which is it?

Note also, with Saxon we recommend doing TransformerFactory.newTemplates() once to compile the stylesheet, and then creating a new Transformer for each transformation using Templates.newTransformer(). Serial reuse of a single Transformer is allowed, but may cause unnecessary accumulation of resources in internal caches.

huangapple
  • 本文由 发表于 2020年9月18日 15:58:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63951574.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定