【发布时间】:2015-10-23 01:52:16
【问题描述】:
我正在尝试在 DocumentBuilderFactory 上设置功能。然而,它只是抛出一个 javax.xml.parsers.ParserConfigurationException 并带有功能名称作为消息:
public void execute() throws Exception
{
// Get the factory.
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// Xerces 1 - http://xerces.apache.org/xerces-j/features.html#external-general-entities
// Xerces 2 - http://xerces.apache.org/xerces2-j/features.html#external-general-entities
setFeature(dbf, "http://xml.org/sax/features/external-general-entities", false);
// Xerces 2 only - http://xerces.apache.org/xerces-j/features.html#external-general-entities
setFeature(dbf, "http://apache.org/xml/features/disallow-doctype-decl", true);
...
}
private void setFeature(DocumentBuilderFactory dbf, String name, boolean value)
{
try {
dbf.setFeature(name, value);
} catch (ParserConfigurationException e) {
e.printStackTrace(); // <- see below
}
}
错误没有提供有用的信息:
javax.xml.parsers.ParserConfigurationException: http://xml.org/sax/features/external-general-entities
at org.apache.harmony.xml.parsers.DocumentBuilderFactoryImpl.setFeature(DocumentBuilderFactoryImpl.java:101)
at com.kounta.printing.epson.EpsonReceiptTranslator.setFeature(EpsonReceiptTranslator.java:76)
at com.kounta.printing.epson.EpsonReceiptTranslator.execute(EpsonReceiptTranslator.java:49)
at com.kounta.printing.epson.EpsonPrintJob$1.run(EpsonPrintJob.java:48)
at com.kounta.util.TaskQueue.internalRun(TaskQueue.java:68)
at com.kounta.util.TaskQueue.access$100(TaskQueue.java:11)
at com.kounta.util.TaskQueue$InternalRunnable.run(TaskQueue.java:79)
at java.lang.Thread.run(Thread.java:841)
有没有办法获得所有支持的功能?还是我做错了什么?这两个功能都会引发异常。
【问题讨论】:
-
我相信底层解析器是 Xerxes,在这种情况下,功能列表可用here
-
@JimGarrison,这是我使用的页面,但即使列出了该功能,它也会引发异常。这就是为什么我想在运行时查看功能列表。
-
那么也许不是 Xerces?使用 IDE,在第一个
setFeature上设置断点并尝试通过检查DocumentBuilderFactory来确定解析器的运行时类。