问题
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