toy
toy

Reputation: 12151

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

Upvotes: 6

Views: 4193

Answers (2)

Paolo
Paolo

Reputation: 1691

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!

Upvotes: 3

constantlearner
constantlearner

Reputation: 5247

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

Upvotes: 3

Related Questions