如何在Java中针对xsd提高速度大xml验证?

我正在尝试针对XSD验证非常XML(~200MB).这花了将近3个小时.我不确定我在这里做错了什么?

    SchemaFactory sf = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(new File(this.productExtraInfoXsd));

    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse(new File(filePath));

    DOMSource domSource = new DOMSource(doc);
    DOMResult result = new DOMResult();

    Validator validator = schema.newValidator();
    validator.validate(domSource, result);
最佳答案
从Marco Tedone查看关于XML解组的这篇文章,请参阅here.基于他,你可以看到Stax

XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(fileInputStream);
Validator validator = schema.newValidator();
validator.validate(new StAXSource(xmlStreamReader));

转载注明原文:如何在Java中针对xsd提高速度大xml验证? - 代码日志