How do I set Entity Expansion Limit in Java

2019-08-03 22:28发布

I ve been asked to parse a large xml file (1.2Gb) using SAX.I m doint my project using Netbeans and I ve ran into a problem.Since the xml has too many entities, Im getting the error: "The parser has encountered more than "64.000" entity expansions in this document; this is the limit imposed by the application."

I must change the EntityExpansionLimit property to a larger number but everything I have tried so far has failed.

Here is my main class:

public class Driver {

public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {


    SAXParserFactory parser = SAXParserFactory.newInstance();
    parser.setNamespaceAware(true);
    SAXParser saxParser = parser.newSAXParser();
    Handler handler = new Handler();
    System.out.println("Please Wait...");
    parser.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, false);

    //Solution 1
    //DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
    //dfactory.setAttribute("http://apache.org/xml/properties/entity-expansion-limit", new Integer("100000"));
    //Result:Property 'http://apache.org/xml/properties/entity-expansion-limit' is not recognized.

    //Solution 2
    //dfactory.setAttribute(JDK_ENTITY_EXPANSION_LIMIT, "100000");
    //Result:The parser has encountered more than "64.000" entity expansions in this document; this is the limit imposed by the application.

    //Solution 3
    //The entityExpansionLimit system property lets existing applications constrain the total number of entity expansions without recompiling the code. 
    //The parser throws a fatal error once it has reached the entity expansion limit. (By default, no limit is set, because such a constraint would make the XML parser incompatible
    //with the XML 1.0 specification.)To set the entity expansion limit using the system property,
    //use an option like the following on the java command line: -DentityExpansionLimit=100000

    saxParser.parse(new File("C:/dblp/dblp.xml"), handler);
    System.out.println("Please Wait...");
    List<Report> reports;
    reports = handler.getList();
    for (Report rep : reports) {
        System.out.println(rep);
    }
}

}

As you can see I ve tried several solutions that I found at oracle's documentation or at similar questions on this site but the property's value just isnt changing.

The only thing that has worked so far is changing the project's properties and setting -DentityExpansionLimit=100000 at VM options.While the project runs smoothly through netbeans,once I try to run the code from command line the same entityExpansionLimit error occurs.

Any suggestions please?

0条回答
登录 后发表回答