Solved

XML / Schema validation - need error node

Posted on 2002-07-09
20
407 Views
Last Modified: 2013-11-19
Hi -

    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"
 
    'Assume failure
    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.
    With objReader
        .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.
        .parse pobjDOMDoc.XML
    End With

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....

Thanks, Nik
0
Comment
Question by:NikWhitfield
  • 7
  • 7
  • 4
  • +2
20 Comments
 

Author Comment

by:NikWhitfield
ID: 7143675
Points increased..
0
 
LVL 27

Expert Comment

by:BigRat
ID: 7146588
Postcard from Rat's Hole:

Having a nice time, wish you were here!

Rat.

Tell me different if I'm stupid, but SAX does not create nodes like DOM. So how can the event signify the node?
0
 
LVL 6

Expert Comment

by:wbradney
ID: 7158325
NikWhitfield,

Of course BigRat is correct.

If you look at the MSXML API, a "node" is actually an IXMLDOMNode object, which gives you a clue as to which mechanism is used to create them. SAX implementations are event-driven, and doesn't create any internal representation of the document (it just tells you when certain conditions are encountered while processing the document in a serial fasion), whereas DOM implementations will parse and internalise the entire document before making it available to applications through the various hierarchically-organised DOM node types.

Regards,
WMB
0
 

Author Comment

by:NikWhitfield
ID: 7158902
Both correct.

My thoughts were that there may be some mechanism between SAX and DOM, such that it is possible to identify a node based on the line number and column number returned in the IVBSAXLocator.

Having looked around a while, nothing like this does exist! I've written something with some degree of success, but not a complete solution by any means.
0
 
LVL 27

Expert Comment

by:BigRat
ID: 7159348
Is the document so big that it couldn't be loaded into a DOM?
0
 

Author Comment

by:NikWhitfield
ID: 7159355
Nope -

  but with the SAX parser, I can get multiple errors on validation, i.e. parse all errors / warnings in one go, as oppose to find an error, raise it, correct problem, find next error, etc.
0
 
LVL 27

Expert Comment

by:BigRat
ID: 7159370
Yes, I see. Then you can fix them all at once rather than one at a time (ever tried programming with Borland Turbo Pascal?).

Still other than "last-tag-seen" there's not much else!
0
 

Author Comment

by:NikWhitfield
ID: 7159394
I think the aforementioned 'last-tag-seen' is the only way to go... unfortunately, it seems that MSXML sometimes returns errors against the wrong attribute, for example ...

Multiple error validation is damned important - for all you know, there are 6000 errors in your schema! You could get pretty pissed off with not knowing the total before you set out to correct them all...
0
 
LVL 5

Expert Comment

by:Netminder
ID: 7197417
A request has been made by the Asker that this question be deleted.

EXPERTS: Please leave your thoughts here; I will return in seven (7) days to resolve the question.

Netminder
CS Moderator
0
 
LVL 27

Expert Comment

by:BigRat
ID: 7200568
OK, but it's a pity about the cheese!
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:NikWhitfield
ID: 7200576
Bigrat,

    sorry, but I managed to implement this functionality myself (with some ultra unpleasant code!). If you'd like to see it, then let me know and I'll forward you the stuff.

Nik
0
 
LVL 6

Expert Comment

by:wbradney
ID: 7200589
Doesn't IVBSAXErrorHandler give you what you need?
0
 
LVL 6

Expert Comment

by:wbradney
ID: 7200592
What I mean to say is, this looks like the same error handler provided by Xerces, etc, and it returns line number, position and node name in its error report.

What other information are you looking for? Maybe there's a cleaner workaround? What is your current workaround?
0
 

Author Comment

by:NikWhitfield
ID: 7200593
Not that I can find
0
 
LVL 6

Expert Comment

by:wbradney
ID: 7200596
Xerces, of course, also has a DOM error handler which would give you what you need if you were able to use it, but I'm guessing that's not an option?
0
 
LVL 6

Expert Comment

by:wbradney
ID: 7200602
Xerces, of course, also has a DOM error handler which would give you what you need if you were able to use it, but I'm guessing that's not an option?
0
 
LVL 6

Expert Comment

by:wbradney
ID: 7200603
Xerces, of course, also has a DOM error handler which would give you what you need if you were able to use it, but I'm guessing that's not an option?
0
 
LVL 6

Expert Comment

by:wbradney
ID: 7200613
Actually, Nik, I realised what I just wrote isn't strictly true -- Xerces (actually the w3 DOM API) uses the SAX error handler to report DOM errors, so you get the exact same error info, but you would get all of them.

I guess the key problem is that DOM won't expose any Node objects until the document is completely parsed and built.
0
 

Author Comment

by:NikWhitfield
ID: 7200624
The basic problem is that SAX will not return an actual DOM node object. I've done a load of gymnastics to find an appropriate node using the error position in the XML file. It's really convoluted, but returns a node in the end, if indeed it was a node which errored (I'm not currently concerned with attributes).
0
 
LVL 6

Accepted Solution

by:
Mindphaser earned 0 total points
ID: 7380257
Points refunded and moved to PAQ

** Mindphaser - Community Support Moderator **
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now