TransformerFactory.newInstance().newTransformer(streamSource) returns null

♀尐吖头ヾ 提交于 2019-12-11 07:00:58

问题


How could that be that TransformerFactory.newInstance().newTransformer(streamSource) returns null. According to javadoc this is not possible: http://download.oracle.com/javase/6/docs/api/javax/xml/transform/TransformerFactory.html#newTransformer(javax.xml.transform.Source)

here's groovy code sample

    def is = new ClassPathResource('xslt/MySpace-Contact.xsl').inputStream
    println is
    def streamSource = new StreamSource(is)
    println streamSource
    def factory = TransformerFactory.newInstance()
    println factory
    def tr = factory.newTransformer(streamSource)
    println tr

Here's the output:

--Output from testTransformation--
java.io.BufferedInputStream@32999f10
javax.xml.transform.stream.StreamSource@399ed64
org.apache.xalan.processor.TransformerFactoryImpl@6eb04214
null


回答1:


Answering my own question. If XSLT file is not valid, that will happen (newTransformer(Source) will return null). I think that should be reported to Oracle so they change the javadoc. It's still POSSIBLE that null is returned.




回答2:


Your script contains a typo. The last statement prints "tf" rather than "tr". "tf" is an uninitialized script variable, hence its value is null.



来源:https://stackoverflow.com/questions/4852168/transformerfactory-newinstance-newtransformerstreamsource-returns-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!