Solved

XML / Schema validation - need error node

Posted on 2002-07-09
20
410 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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
 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
AngularJS directive issue 6 70
Coldfusion- Create and save form elements in Database 7 72
How do I bind the results to a grid 3 35
XML XSL Message 3 21
Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

805 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