Search code examples
javaxmlsaxparser

org.xml.sax.SAXParseException: The parser has encountered more than "64 000" entity expansions in this document


I am parsing large XML(72MB-170K entries) file using SAX XML parser:

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);

Error:

org.xml.sax.SAXParseException: The parser has encountered more than "64 000" entity expansions in this document; this is the limit imposed by the application.

How to limit SAX parser entries(for example to 1000 entries)?


Solution

  • Solved problem by setting 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);