我正在使用Stax解析器来解析一个非常大的xml文件,这是我编写的代码
public class XmlWrite {
public static void main(String[] args) throws
IOException,XMLStreamException {
FileWriter fw = null;
FileReader page = new FileReader("pages142");
XMLInputFactory factory = XMLInputFactory.newInstance();
//factory.setProperty(XMLConstants.FEATURE_SECURE_PROCESSING,false);
XMLEventReader eventReader = factory.createXMLEventReader(page);
boolean flag = false;
int no = 1;
while(eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
.........
此代码适用于小型输入文件,但对于大型文件,它会出现以下错误
Exception in thread "main" javax.xml.stream.XMLStreamException:
ParseError at [row,col]:[44018907,204]
Message: JAXP00010004: The accumulated size of entities is
"50,000,001" that exceeded the "50,000,000" limit set by
"FEATURE_SECURE_PROCESSING".
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:604) at com.sun.xml.internal.stream.XMLEventReaderImpl.nextEvent(XMLEventReaderImpl.java:83) at XmlWrite.main(XmlWrite.java:28)
我尝试了factory.setProperty,但这也没有用。有没有办法在整个使用jvm时禁用安全处理?
答案 0 :(得分:1)
尝试
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING,false)
参考:https://docs.oracle.com/javase/tutorial/jaxp/properties/scope.html
答案 1 :(得分:0)
使用此变通办法有帮助
java -DentityExpansionLimit = 2147480000 -DtotalEntitySizeLimit = 2147480000 -Djdk.xml.totalEntitySizeLimit = 2147480000代码名称