我在设置 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); 

请帮我解决这个问题。

请您参考如下方法:

我也未能调用 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) 

希望对您有帮助。


评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!