Results 1 to 8 of 8

Thread: Validate XML against XSD/XDR in VB

  1. #1

    Thread Starter
    Fanatic Member pradeepkrao's Avatar
    Join Date
    Sep 2001
    Location
    New Jersey
    Posts
    534

    Validate XML against XSD/XDR in VB

    Hi,

    Could anybody help me out in validating the XML against the schema...

    I have found some code here.. But I dont have DOMDocument40.. I have DOMDocument30...

    Please help me out...

    Thanks in advance,
    Pradeep
    Learn by others experience as you cannot live long to experience them all.
    www.freewebs.com/pradeepkrao

    LOOK AT MY GAMES AT MY WEB SITE.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Doesn't MSXML support Schema? If not, you'll have problems.

    Maybe the COM bindings of Xerces can help you. Warning: this needs a C++ compiler!
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    MSXML supports either a XSD (schema) or a DTD file. I'm guessing that it should not matter if you have MSXML3 or MSXML4. MSXML4 supports validation and I assume MSXML3 does as well. [Later edit: It's not supported in MSXML3] If it doesn't you can always download MSXML4 (you should anyhow).

    Here's the code I use. Except for the error routine and other stuff specific to my app, I would guess you would only need to change XMLSchemaCache40 to XMLSchemaCache30.

    VB Code:
    1. Public Sub ValidateXML()
    2. '***************************************************************************
    3. 'Purpose: Validate the modified xml file against the schema
    4. 'Inputs:  None
    5. 'Outputs: True if OK, False if not.
    6. '***************************************************************************
    7.  
    8.     Dim oxmlSchema As MSXML2.XMLSchemaCache40
    9.     Dim oxmlValidateDoc As MSXML2.DOMDocument40
    10.     Dim strText As String
    11.    
    12.     Screen.MousePointer = vbHourglass
    13.    
    14.     On Error GoTo ErrorRoutine
    15.    
    16.     'Create the schema cache and add the schema to it.
    17.     Set oxmlSchema = New MSXML2.XMLSchemaCache40
    18.     oxmlSchema.Add "", MySchemaPathAndName
    19.    
    20.     'Create an XML DOMDocument object.
    21.     Set oxmlValidateDoc = New MSXML2.DOMDocument40
    22.    
    23.     'Assign the schema cache to the schemas collection.
    24.     Set oxmlValidateDoc.schemas = oxmlSchema
    25.    
    26.     'Load the xml file as the DOM document.
    27.     oxmlValidateDoc.async = False
    28.     oxmlValidateDoc.Load gstrTempFileName
    29.    
    30.     Screen.MousePointer = vbNormal
    31.    
    32.     'Return validation results in message to the user.
    33.     If oxmlValidateDoc.parseError.errorCode <> 0 Then
    34.         If oxmlValidateDoc.parseError.errorCode = -2146697210 Then
    35.             Err.Raise 1004
    36.         Else
    37.             ' Remove tabs from the text
    38.             strText = Replace(oxmlValidateDoc.parseError.srcText, Chr$(9), "")
    39.             frmValidate.lblResults.Alignment = vbLeftJustify
    40.             frmValidate.lblResults.Caption = _
    41.                     "Error: " & oxmlValidateDoc.parseError.reason _
    42.                     & vbCrLf _
    43.                     & "Found in line number " & oxmlValidateDoc.parseError.Line _
    44.                     & "  line position " & oxmlValidateDoc.parseError.linepos & "." _
    45.                     & vbCrLf & vbCrLf _
    46.                     & strText _
    47.                     & vbCrLf & vbCrLf _
    48.                     & "(Other errors may be present.)"
    49.             frmValidate.Show vbModal
    50.         End If
    51.     Else
    52.         frmValidate.lblResults.Alignment = vbCenter
    53.         frmValidate.lblResults.Caption = vbCrLf & vbCrLf _
    54.                                        & "Knowledge base validation passed"
    55.         frmValidate.Show vbModal
    56.     End If
    57.    
    58.     Set oxmlSchema = Nothing
    59.     Set oxmlValidateDoc = Nothing
    60.  
    61.     Exit Sub
    62.    
    63. ErrorRoutine:
    64.  
    65.     If Err.Number = 1004 Then
    66.         MsgBox ResolveResString(resMissingTempFile), _
    67.                vbExclamation + vbOKOnly, _
    68.                ResolveResString(resErrorTitle, "|1", "Validation")
    69.     Else
    70.         DisplayError "ValidateXML"
    71.     End If
    72.    
    73. End Sub

  4. #4

    Thread Starter
    Fanatic Member pradeepkrao's Avatar
    Join Date
    Sep 2001
    Location
    New Jersey
    Posts
    534

    Hi

    Doesnt work..

    I have an xsd file and an xml file.

    I got to validate the xml file against the xsd file..

    While executing the below..
    VB Code:
    1. Set oxmlSchema = New MSXML2.XMLSchemaCache40
    2.     oxmlSchema.Add "", "C:\Viewer\GenSchema.xsd"

    I get an error... Err.Number = -2147467259

    Please help me out..

    If I try to validate this xsd against the xml in XML Spy. It works fine..
    Learn by others experience as you cannot live long to experience them all.
    www.freewebs.com/pradeepkrao

    LOOK AT MY GAMES AT MY WEB SITE.

  5. #5

  6. #6

    Thread Starter
    Fanatic Member pradeepkrao's Avatar
    Join Date
    Sep 2001
    Location
    New Jersey
    Posts
    534

    Hi

    I am using the same code which you have posted..
    Learn by others experience as you cannot live long to experience them all.
    www.freewebs.com/pradeepkrao

    LOOK AT MY GAMES AT MY WEB SITE.

  7. #7

  8. #8

    Thread Starter
    Fanatic Member pradeepkrao's Avatar
    Join Date
    Sep 2001
    Location
    New Jersey
    Posts
    534

    Hi

    Here is the code..

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim xmlschema As MSXML2.XMLSchemaCache40
    3. Dim xmldom As MSXML2.DOMDocument40
    4.  
    5.     Set xmlschema = New MSXML2.XMLSchemaCache40
    6.    
    7.     xmlschema.Add "", "C:\D-drive\HELP_Temp\EMohan\ASNViewer\GenSchema.xsd"
    8.    
    9.     Set xmldom = New MSXML2.DOMDocument40
    10.    
    11.     xmldom.async = False
    12.    
    13.     Set xmldom.schemas = xmlschema
    14.    
    15.     xmldom.Load "C:\D-drive\HELP_Temp\EMohan\ASNViewer\Pradeep.xml"
    16.    
    17.     If xmldom.parseError.errorCode <> 0 Then
    18.       MsgBox xmldom.parseError.errorCode & " " & xmldom.parseError.reason
    19.     Else
    20.       MsgBox "No Error"
    21.     End If
    22.      
    23.     Set xmldom = Nothing
    24.     Set xmlschema = Nothing
    25.  
    26. End Sub
    27.  
    28. Public Sub ValidateXML()
    29. '***************************************************************************
    30. 'Purpose: Validate the modified xml file against the schema
    31. 'Inputs:  None
    32. 'Outputs: True if OK, False if not.
    33. '***************************************************************************
    34.  
    35.     Dim oxmlSchema As MSXML2.XMLSchemaCache40
    36.     Dim oxmlValidateDoc As MSXML2.DOMDocument40
    37.     Dim strText As String
    38.    
    39.     On Error GoTo ErrorRoutine
    40.    
    41.     'Create the schema cache and add the schema to it.
    42.     Set oxmlSchema = New MSXML2.XMLSchemaCache40
    43.     oxmlSchema.Add "", "C:\D-drive\HELP_Temp\EMohan\ASNViewer\GenSchema.xsd"
    44.    
    45.     'Create an XML DOMDocument object.
    46.     Set oxmlValidateDoc = New MSXML2.DOMDocument40
    47.    
    48.     'Assign the schema cache to the schemas collection.
    49.     Set oxmlValidateDoc.schemas = oxmlSchema
    50.    
    51.     'Load the xml file as the DOM document.
    52.     oxmlValidateDoc.async = False
    53.     oxmlValidateDoc.Load "C:\D-drive\HELP_Temp\EMohan\ASNViewer\good.xml"
    54.    
    55.     'Return validation results in message to the user.
    56.     If oxmlValidateDoc.parseError.errorCode <> 0 Then
    57.         If oxmlValidateDoc.parseError.errorCode = -2146697210 Then
    58.             Err.Raise 1004
    59.         Else
    60.             ' Remove tabs from the text
    61.             strText = Replace(oxmlValidateDoc.parseError.srcText, Chr$(9), "")
    62.             frmValidate.lblResults.Alignment = vbLeftJustify
    63.             frmValidate.lblResults.Caption = _
    64.                     "Error: " & oxmlValidateDoc.parseError.reason _
    65.                     & vbCrLf _
    66.                     & "Found in line number " & oxmlValidateDoc.parseError.Line _
    67.                     & "  line position " & oxmlValidateDoc.parseError.linepos & "." _
    68.                     & vbCrLf & vbCrLf _
    69.                     & strText _
    70.                     & vbCrLf & vbCrLf _
    71.                     & "(Other errors may be present.)"
    72.             frmValidate.Show vbModal
    73.         End If
    74.     Else
    75.         frmValidate.lblResults.Alignment = vbCenter
    76.         frmValidate.lblResults.Caption = vbCrLf & vbCrLf _
    77.                                        & "Knowledge base validation passed"
    78.         frmValidate.Show vbModal
    79.     End If
    80.    
    81.     Set oxmlSchema = Nothing
    82.     Set oxmlValidateDoc = Nothing
    83.  
    84.     Exit Sub
    85.    
    86. ErrorRoutine:
    87.  
    88.     If Err.Number = 1004 Then
    89.         MsgBox "Error"
    90.         'MsgBox ResolveResString(resMissingTempFile), _
    91.         '       vbExclamation + vbOKOnly, _
    92.         '       ResolveResString(resErrorTitle, "|1", "Validation")
    93.    
    94.     Else
    95.         'DisplayError "ValidateXML"
    96.         MsgBox "Valid"
    97.     End If
    98.    
    99. End Sub
    100.  
    101. Private Sub Command2_Click()
    102.     ValidateXML
    103. End Sub

    Its working in VC++.... Well I will make a dll then..
    Learn by others experience as you cannot live long to experience them all.
    www.freewebs.com/pradeepkrao

    LOOK AT MY GAMES AT MY WEB SITE.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width