我正在尝试在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)
有没有办法获得所有支持的功能?或者我做错了什么?这两个功能都是例外。
答案 0 :(得分:1)
所以事实证明默认情况下出货的XML解析器是垃圾和不完整的(因为这是android而不奇怪),我最终发现了这个问题:
尽管文档要求所有XML解析器都支持FEATURE_SECURE_PROCESSING - http://developer.android.com/reference/javax/xml/parsers/SAXParserFactory.html#setFeature(java.lang.String,%20boolean) - 但事实并非如此。
解决方案是我将不得不使用不同的库来处理XML的解析。