Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
like image 900
toy Avatar asked Nov 04 '13 16:11

toy


2 Answers

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));
like image 133
constantlearner Avatar answered Nov 11 '22 17:11

constantlearner


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!

like image 23
Paolo Avatar answered Nov 11 '22 19:11

Paolo