Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中设置实体扩展限制_Java_Xml_Parsing_Netbeans - Fatal编程技术网

如何在Java中设置实体扩展限制

如何在Java中设置实体扩展限制,java,xml,parsing,netbeans,Java,Xml,Parsing,Netbeans,我被要求使用SAX解析一个大的xml文件(1.2Gb)。我使用Netbeans进行项目时遇到了一个问题。由于xml有太多的实体,我得到了一个错误:“解析器在此文档中遇到了超过“64.000”的实体扩展;这是应用程序施加的限制。” 我必须将EntityExpansionLimit属性更改为一个更大的数字,但到目前为止我尝试的所有操作都失败了 这是我的主要课程: public class Driver { public static void main(String[] args) throws

我被要求使用SAX解析一个大的xml文件(1.2Gb)。我使用Netbeans进行项目时遇到了一个问题。由于xml有太多的实体,我得到了一个错误:“解析器在此文档中遇到了超过“64.000”的实体扩展;这是应用程序施加的限制。”

我必须将EntityExpansionLimit属性更改为一个更大的数字,但到目前为止我尝试的所有操作都失败了

这是我的主要课程:

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);
    }
}
公共类驱动程序{
公共静态void main(字符串[]args)抛出ParserConfiguration异常、SAXException、IOException{
SAXParserFactory解析器=SAXParserFactory.newInstance();
setNamespaceAware(true);
SAXParser SAXParser=parser.newSAXParser();
Handler=newhandler();
System.out.println(“请稍候…”);
setFeature(xmlstants.FEATURE\u SECURE\u PROCESSING,false);
//解决方案1
//DocumentBuilderFactory dfactory=DocumentBuilderFactory.newInstance();
//dfactory.setAttribute(“http://apache.org/xml/properties/entity-expansion-limit,新整数(“100000”);
//结果:物业http://apache.org/xml/properties/entity-expansion-limit“不被承认。
//解决方案2
//setAttribute(JDK_实体_扩展_限制,“100000”);
//结果:解析器在此文档中遇到了超过“64.000”个实体扩展;这是应用程序施加的限制。
//解决方案3
//entityExpansionLimit系统属性允许现有应用程序约束实体扩展的总数,而无需重新编译代码。
//一旦解析器达到实体扩展限制,它就会抛出一个致命错误。(默认情况下,没有设置限制,因为这样的约束会使XML解析器不兼容。)
//要使用system属性设置实体扩展限制,
//在java命令行上使用如下选项:-DentityExpansionLimit=100000
parse(新文件(“C:/dblp/dblp.xml”),handler);
System.out.println(“请稍候…”);
列出报告;
reports=handler.getList();
对于(报告代表:报告){
系统输出打印项次(代表);
}
}
}

正如您所见,我已经尝试了几种解决方案,这些解决方案可以在oracle的文档中找到,也可以在本网站上的类似问题中找到,但该属性的价值没有改变

到目前为止,唯一有效的方法是更改项目的属性,并在VM选项处设置-DentityExpansionLimit=100000。虽然项目通过netbeans平稳运行,但一旦我尝试从命令行运行代码,就会出现相同的entityExpansionLimit错误


有什么建议吗?

什么版本的Java?当您从命令行运行应用程序时,是否将
-DentityExpansionLimit=100000
传递给jvm?我使用的是最新版本1.7.0\u 25。Guido我将其作为独立命令传递,这就是它不工作的原因。Tx很多,问题解决了。为了解决这个问题,关于如何通过我的代码更改属性值有什么想法吗?这里的回复太晚了,我今天遇到了这个问题。这对我很有帮助。