我在设置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);
请帮我解决这个问题。
答案 0 :(得分:0)
我也未能调用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)
希望对您有帮助。