DocumentBuilderFactory 获得可用功能? [英] DocumentBuilderFactory get available features?
问题描述
我正在尝试在 DocumentBuilderFactory
上设置功能.但是,它只是抛出一个 javax.xml.parsers.ParserConfigurationException
并带有特性名称作为消息:
I am trying to set features on a DocumentBuilderFactory
. However, it just throws a javax.xml.parsers.ParserConfigurationException
with the features name as the message:
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
}
}
错误没有提供有用的信息:
The error gives no useful information:
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)
有没有办法获得所有支持的功能?还是我做错了什么?这两个功能都会抛出异常.
Is there a way to get all the supported features? Or am I doing something wrong? Both features throw an exception.
推荐答案
所以事实证明默认提供的 XML 解析器是垃圾和不完整的(这并不奇怪,因为这是 android),我最终发现了这个问题:
So it turns out that the XML parser that is shipped by default is rubbish and incomplete (not surprising since this is android) and I found the issue eventually:
因此,尽管文档要求所有 XML 解析器都支持 FEATURE_SECURE_PROCESSING -http://developer.android.com/reference/javax/xml/parsers/SAXParserFactory.html#setFeature(java.lang.String,%20boolean) - 确实如此不是.
So despite the documentation requiring that all XML parsers support FEATURE_SECURE_PROCESSING -http://developer.android.com/reference/javax/xml/parsers/SAXParserFactory.html#setFeature(java.lang.String,%20boolean) - this does not.
解决方案是我将不得不使用不同的库来处理 XML 的解析.
The solution is I will have to use a different library to handle the parsing of the XML.
这篇关于DocumentBuilderFactory 获得可用功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!