TransformerConfigurationException:无法为变压器的功能(Transf

2019-09-30 01:06发布

我得到以下错误而设置的TransformerFactory的功能。

javax.xml.transform.TransformerConfigurationException: Cannot set the feature 'http://apache.org/xml/features/disallow-doctype-decl' on this TransformerFactory.
        at org.apache.xalan.processor.TransformerFactoryImpl.setFeature(TransformerFactoryImpl.java:416)

代码段是::

public static TransformerFactory mytFactory;

mytFactory = TransformerFactory.newInstance();
mytFactory.setFeature(“http://apache.org/xml/features/disallow-doctype-decl“, true);

请帮我解决这个问题。

Answer 1:

我也没有打电话TransformerFactoryImpl的方法“setFeature”。 而且我发现其他方式使用的XMLReader如下设置功能:

XMLReader reader = XMLReaderFactory.createXMLReader();
reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);

Source xmlSource = new SAXSource(reader, new InputSource(new FileInputStream(xmlFile)));
Transformer transformer = TransformerFactory.newInstance().newTransformer();
StreamResult result = new StreamResult(new StringWriter());
transformer.transform(xmlSource, result);

该设置后,如果输入有内部DOCTYPE标签,我们将有以下情况除外:

javax.xml.transform.TransformerException: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 10; DOCTYPE is disallowed when the feature "http://apache.org/xml/features/disallow-doctype-decl" set to true.
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:749)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:351)
    at ch.ofwi.pa.webservice.provideraccounting.impl.handler.SchemaValidationHandler.main(SchemaValidationHandler.java:227)
Caused by: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 10; DOCTYPE is disallowed when the feature "http://apache.org/xml/features/disallow-doctype-decl" set to true.
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1239)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:668)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:737)

希望这是对你有帮助。



文章来源: TransformerConfigurationException: Cannot set the feature for Transformer