XML / Schema validation - need error node
Posted on 2002-07-09
I'm running validation for an XML file against an xsd schema using MSXML4. I do this using the following code:
Dim objReader As New MSXML2.SAXXMLReader40 '- Create a SAX reader.
Dim objValidator As clsXMLEventHandler '- Create an instance of the class module that implements the IVBSAXErrorHandler interface
Const VALIDATE_FEATURE As String = "schema-validation"
Const EXHAUSTIVE_FEATURE As String = "exhaustive-errors"
Const VALIDATE_TYPE_PROPERTY As String = "schemas"
ValidateXML = False
'Instantiate the event handler
Set objValidator = New clsXMLEventHandler
'Setup the validator
objValidator.XML = pobjDOMDoc.XML
'Configure the SAX reader to validate the XML document.
.putFeature VALIDATE_FEATURE, True
.putFeature EXHAUSTIVE_FEATURE, True
.putProperty VALIDATE_TYPE_PROPERTY, mobjSchemaCache
'Assign an instance of the MyValidator class to the errorHandler property of the SAX reader.
Set .ErrorHandler = objValidator
'Parse and validate the xml.
This uses an event handler class which implements IVBSAXErrorHandler. This catches validation errors in subroutines like this one:
IVBSAXErrorHandler_error(ByVal oLocator As MSXML2.IVBSAXLocator, strErrorMessage As String, ByVal nErrorCode As Long)
This is great and gives me some useful info. However, what I could really do with is the Node object for which the error was thrown.
Does anyone know how I would go about getting hold of the node object? I've got the line number and column, and a copy of the .XML text - how would I go about finding the required node??
Answers on a postcard....