Validating an XML file with a schema file
-
I am using QXmlSchemaValidator to validate an XML file. As far as I can tell, up to only one error is logged by QXmlSchemaValidator. Is there a way to get a list of all validation errors? Thanks.
-
If have to admit that I haven't used QXmlSchemaValidator before but a quick look at the documentation reveals "QXmlSchemaValidator::setMessageHandler()":http://doc.qt.nokia.com/latest/qxmlschemavalidator.html#setMessageHandler.
[quote]
Changes the message handler for this QXmlSchemaValidator to handler. The schema validator sends all parsing and validation messages to this message handler. QXmlSchemaValidator does not take ownership of handler.
[/quote]Is this not sufficient for you or does it not work as expected?
-
I did set a message handler. My handler class has an STL vector data member that keeps track of the errors. The overridden handleMessage() method inserts an error message into this vector data member every time it is called. However, even though I know the XML file has more then 1 error, only the first error gets inserted into the vector data member. From what I can tell, QXmlSchemaValidator::validate() returns after the first XML error is encountered.
-
[quote author="banerkx" date="1316813569"]From what I can tell, QXmlSchemaValidator::validate() returns after the first XML error is encountered. [/quote]
That would actually seem pretty reasonable. After the first XML error, the rest of the document is then suspect and, thus, potentially unpredictable to validate further with any accuracy. No different than if you have a syntax problem in a C++ file; the compiler fails with an error upon the first bad line of code.
-
@mllong: this happens in js or QML but a good compiler set a list of syntax error too. The problems in this case maybe that the further syntax errors after the first may depend from it (i.e. a classic unabalanced parentheses).
If it is possible for you to explore the original xmls file by sections you can validate them in a loop to manage all the file despite if one or more - coherent - sections are wrong and can't be validated. This requires a pre-parse operation dividing the entire xml file in sub elements... Sure it is complicated.
-
@Alicemirror: I agree completely. However, I was just speaking in the context of the default behavior of QXmlSchemaValidator, and supposing as to why it might stop after the first error. That is to say, its behavior seems appropriate for a simple validator (i.e., XML either is or is not valid), but not an in-depth analysis tool.
A validator is good for a quick "Not valid!" in the events where you just need to check while processing. If you're checking XML on-the-fly in a lightweight app, you're typically more concerned with a speedy Yes or No, rather than asking why. I think that QXmlSchemaValidator is designed to play that role well.
-
@mlong: then, I agree with you. Excluding strange and rare cases, when a malformed xml error is retrieved it is sufficient for me to suppose that something is wrong in the data.