簡體   English   中英

從Access 2007到2010的移植無法使用XSD驗證XML

[英]Porting from Access 2007 to 2010 fails on validating an XML using an XSD

我正在針對XSD驗證XML。

這是我使用的功能:

Public Function ValidaXML(ByVal strXMLPath As String, _
                          ByVal strXSDPath As String) As Boolean
    Dim objSchemas As MSXML2.XMLSchemaCache40
    Dim objXML As MSXML2.DOMDocument40
    Dim objXSD As MSXML2.DOMDocument40
    Dim strNamespace As String
    Dim objErr As MSXML2.IXMLDOMParseError

    ' load XSD as DOM to populate in Schema Cache
    Set objXSD = New MSXML2.DOMDocument40
    objXSD.async = False
    If Not objXSD.Load(strXSDPath) Then
       Err.Raise 1, "ValidaXML", "Load XSD failed: " & objXSD.parseError.reason
    Else
        ' get namespace name from XSD targetNamespace attribute
        strNamespace = objXSD.documentElement.getAttribute("targetNamespace")
    End If

    ' populate schema cache
    Set objSchemas = New MSXML2.XMLSchemaCache40
    objSchemas.Add strNamespace, objXSD

    ' load XML file (without validation - that comes later)
    Set objXML = New MSXML2.DOMDocument40
    objXML.async = False
    objXML.validateOnParse = False
    objXML.resolveExternals = False

    ' load XML, without any validation
    If Not objXML.Load(strXMLPath) Then
       Err.Raise 1, "ValidaXML", "Load XML failed: " & objXML.parseError.reason
    End If

    ' bind Schema Cache to DOM
    Set objXML.schemas = objSchemas

    ' does this XML measure up?
    Set objErr = objXML.Validate()

    ' any good?
    ValidaXML = (objErr.errorCode = 0)
    If objErr.errorCode <> 0 Then
       Err.Raise 1, "ValidaXML", objErr.reason
    End If
End Function

我使用的指令是:

Private Sub valida_Click()
Call ValidaXML("C:\xq10\q19\recibos.xml", "C:\xq10\xml\pain.008.001.02.xsd")
End Sub

使用Microsoft Access 2007,該功能正常運行。 但是,當我使用Access 2010時,第一行出現錯誤:

Set objXSD = New MSXML2.DOMDocument40

我收到的錯誤是:

運行時錯誤“ 429”:ActiveX組件無法創建對象

我非常感謝您可以提供的任何幫助。

盡管您還沒有給出錯誤,但我會猜測這是一個參考錯誤,因為您可以在實例化行上得到它。

MSDN帖子顯示了如何在VBA中引用Office 2010的MSXML 4.0 添加引用后,您的代碼應與Access 2007中的一樣。

編輯 :您說您收到錯誤429,它確實表明無法創建該對象。 您使用MSXML 4.0,默認情況下並非在每個Windows系統上都安裝MSXML 4.0。 您說您引用了MSXML 6.0,在這種情況下,您應該更新代碼以反映此情況:

Set objXSD = New MSXML2.DOMDocument60

您的代碼中可能還有其他地方需要應用此類更改。 提示:表示“ 40”時表示“ 4.0”,不表示時表示“ 3.0”,表示“ 60”時表示“ 6.0”。 如果要讓代碼在任何最新的Windows系統上運行,請避免使用3.0和6.0以外的任何其他版本。

在SO上,這是一個很好的答案,它解釋了為什么以及何時應該選擇什么版本的MSXML

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM