TransformerConfigurationException: Cannot set the feature for Transformer

我只是一个虾纸丫 提交于 2019-12-11 04:29:25

问题


i'm getting below error while setting the feature for 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)

Code snippet is::

public static TransformerFactory mytFactory;

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

please help me to solve this issue.


回答1:


I have also failed to call method "setFeature" of TransformerFactoryImpl. And I found other way to set feature by using XMLReader as below:

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);

After that setting, if the input has DOCTYPE tag inside, we will have following exception:

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)

Hope that it's helpful for you.



来源:https://stackoverflow.com/questions/50234862/transformerconfigurationexception-cannot-set-the-feature-for-transformer

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