How to improve speed large xml validation against xsd in Java?

我是研究僧i 提交于 2021-01-27 05:21:30

问题


I'm trying to validate a very XML (~200MB) against XSD. It's taking almost 3 hours. I'm not sure what am I doing wrong here?

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

回答1:


check this article on XML unmarshalling from Marco Tedone see here. Based on his you can see Stax

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



回答2:


Have a look at this stackoverflow topic. Here is written that:

You should not use the DOMParser to validate a document (unless your goal is to create a document object model anyway). This will start creating DOM objects as it parses the document - wasteful if you aren't going to use them.

Maybe it will be useful!



来源:https://stackoverflow.com/questions/19772165/how-to-improve-speed-large-xml-validation-against-xsd-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!