org.xml.sax.SAXParseException:解析器在本文档中遇到了超过“64 000”的实体扩展

时间:2014-06-05 11:32:25

标签: java xml saxparser

我使用SAX XML解析器解析大型XML(72MB-170K条目)文件:

SAXParser saxParser = saxParserFactory.newSAXParser();
SAXParser saxParser = saxParserFactory.newSAXParser();
MyHandler handler = new MyHandler();

//THIS LINE IS CAUSING THE ERROR
saxParser.parse(new File("JMdict"), handler);
// Get entries list
List<Entry> entryList = handler.getEmpList();
// print entry information
for (Entry ent : entryList)
    System.out.println(ent);

错误:

  

org.xml.sax.SAXParseException:解析器遇到了多个   本文件中的“64 000”实体扩展;这是强加的限制   通过申请。

如何限制SAX解析器条目(例如限制为1000个条目)?

1 个答案:

答案 0 :(得分:1)

通过设置EntityExpansionLimit

解决了问题
    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    SAXParser saxParser = saxParserFactory.newSAXParser();
    org.apache.xerces.util.SecurityManager mgr = new org.apache.xerces.util.SecurityManager();
    mgr.setEntityExpansionLimit(1000000);
    saxParser.setProperty("http://apache.org/xml/properties/security-manager", mgr);
    MyHandler handler = new MyHandler();

    // THIS LINE IS CAUSING THE ERROR
    saxParser.parse(new File("JMdict"), handler);
    // Get entries list
    List<Entry> entryList = handler.getEmpList();
    // print entry information
    for (Entry ent : entryList)
        System.out.println(ent);