问题
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