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
Printable View
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
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!
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:
Public Sub ValidateXML() '*************************************************************************** 'Purpose: Validate the modified xml file against the schema 'Inputs: None 'Outputs: True if OK, False if not. '*************************************************************************** Dim oxmlSchema As MSXML2.XMLSchemaCache40 Dim oxmlValidateDoc As MSXML2.DOMDocument40 Dim strText As String Screen.MousePointer = vbHourglass On Error GoTo ErrorRoutine 'Create the schema cache and add the schema to it. Set oxmlSchema = New MSXML2.XMLSchemaCache40 oxmlSchema.Add "", MySchemaPathAndName 'Create an XML DOMDocument object. Set oxmlValidateDoc = New MSXML2.DOMDocument40 'Assign the schema cache to the schemas collection. Set oxmlValidateDoc.schemas = oxmlSchema 'Load the xml file as the DOM document. oxmlValidateDoc.async = False oxmlValidateDoc.Load gstrTempFileName Screen.MousePointer = vbNormal 'Return validation results in message to the user. If oxmlValidateDoc.parseError.errorCode <> 0 Then If oxmlValidateDoc.parseError.errorCode = -2146697210 Then Err.Raise 1004 Else ' Remove tabs from the text strText = Replace(oxmlValidateDoc.parseError.srcText, Chr$(9), "") frmValidate.lblResults.Alignment = vbLeftJustify frmValidate.lblResults.Caption = _ "Error: " & oxmlValidateDoc.parseError.reason _ & vbCrLf _ & "Found in line number " & oxmlValidateDoc.parseError.Line _ & " line position " & oxmlValidateDoc.parseError.linepos & "." _ & vbCrLf & vbCrLf _ & strText _ & vbCrLf & vbCrLf _ & "(Other errors may be present.)" frmValidate.Show vbModal End If Else frmValidate.lblResults.Alignment = vbCenter frmValidate.lblResults.Caption = vbCrLf & vbCrLf _ & "Knowledge base validation passed" frmValidate.Show vbModal End If Set oxmlSchema = Nothing Set oxmlValidateDoc = Nothing Exit Sub ErrorRoutine: If Err.Number = 1004 Then MsgBox ResolveResString(resMissingTempFile), _ vbExclamation + vbOKOnly, _ ResolveResString(resErrorTitle, "|1", "Validation") Else DisplayError "ValidateXML" End If End Sub
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:
Set oxmlSchema = New MSXML2.XMLSchemaCache40 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..
Can you post your whole validation routine?
I am using the same code which you have posted..
I would still like to see your code because the code I posted works for me. And just to make sure, are you using MSXML4?
Here is the code..
VB Code:
Private Sub Command1_Click() Dim xmlschema As MSXML2.XMLSchemaCache40 Dim xmldom As MSXML2.DOMDocument40 Set xmlschema = New MSXML2.XMLSchemaCache40 xmlschema.Add "", "C:\D-drive\HELP_Temp\EMohan\ASNViewer\GenSchema.xsd" Set xmldom = New MSXML2.DOMDocument40 xmldom.async = False Set xmldom.schemas = xmlschema xmldom.Load "C:\D-drive\HELP_Temp\EMohan\ASNViewer\Pradeep.xml" If xmldom.parseError.errorCode <> 0 Then MsgBox xmldom.parseError.errorCode & " " & xmldom.parseError.reason Else MsgBox "No Error" End If Set xmldom = Nothing Set xmlschema = Nothing End Sub Public Sub ValidateXML() '*************************************************************************** 'Purpose: Validate the modified xml file against the schema 'Inputs: None 'Outputs: True if OK, False if not. '*************************************************************************** Dim oxmlSchema As MSXML2.XMLSchemaCache40 Dim oxmlValidateDoc As MSXML2.DOMDocument40 Dim strText As String On Error GoTo ErrorRoutine 'Create the schema cache and add the schema to it. Set oxmlSchema = New MSXML2.XMLSchemaCache40 oxmlSchema.Add "", "C:\D-drive\HELP_Temp\EMohan\ASNViewer\GenSchema.xsd" 'Create an XML DOMDocument object. Set oxmlValidateDoc = New MSXML2.DOMDocument40 'Assign the schema cache to the schemas collection. Set oxmlValidateDoc.schemas = oxmlSchema 'Load the xml file as the DOM document. oxmlValidateDoc.async = False oxmlValidateDoc.Load "C:\D-drive\HELP_Temp\EMohan\ASNViewer\good.xml" 'Return validation results in message to the user. If oxmlValidateDoc.parseError.errorCode <> 0 Then If oxmlValidateDoc.parseError.errorCode = -2146697210 Then Err.Raise 1004 Else ' Remove tabs from the text strText = Replace(oxmlValidateDoc.parseError.srcText, Chr$(9), "") frmValidate.lblResults.Alignment = vbLeftJustify frmValidate.lblResults.Caption = _ "Error: " & oxmlValidateDoc.parseError.reason _ & vbCrLf _ & "Found in line number " & oxmlValidateDoc.parseError.Line _ & " line position " & oxmlValidateDoc.parseError.linepos & "." _ & vbCrLf & vbCrLf _ & strText _ & vbCrLf & vbCrLf _ & "(Other errors may be present.)" frmValidate.Show vbModal End If Else frmValidate.lblResults.Alignment = vbCenter frmValidate.lblResults.Caption = vbCrLf & vbCrLf _ & "Knowledge base validation passed" frmValidate.Show vbModal End If Set oxmlSchema = Nothing Set oxmlValidateDoc = Nothing Exit Sub ErrorRoutine: If Err.Number = 1004 Then MsgBox "Error" 'MsgBox ResolveResString(resMissingTempFile), _ ' vbExclamation + vbOKOnly, _ ' ResolveResString(resErrorTitle, "|1", "Validation") Else 'DisplayError "ValidateXML" MsgBox "Valid" End If End Sub Private Sub Command2_Click() ValidateXML End Sub
Its working in VC++.... Well I will make a dll then..