0

为了在 MS Excel VBA 中解析我的 XML 文档,我必须使用 MSXML2.DOMDocument.6.0。

XPath 语句如下所示:

Public xml_document As Object
Public xml_namespace_uri As String
...
    xml_namespace_uri = "urn:iso:std:iso:20022:tech:xsd:pain.008.001.02"

    Set xml_document = CreateObject("MSXML2.DOMDocument")

    xml_document.async = False
    xml_document.validateOnParse = True
    xml_document.LoadXML _
        "<?xml version=""1.0"" encoding=""UTF-8""?>" & _
        "<Document xmlns=""" & xml_namespace_uri & """ " & _
        "xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""/>"

    xml_document.SelectSingleNode("/Document").appendChild _
        xml_document.createNode(1, "CstmrDrctDbtInitn", xml_namespace_uri)

工作正常,但一旦我更换

    Set xml_document = CreateObject("MSXML2.DOMDocument")

经过

    Set xml_document = CreateObject("MSXML2.DOMDocument.6.0")

XPath 语句失败并退出包含子。谁能解释我在这里做错了什么?

2020-09-28 12:00:00

在阅读了所有建议和评论后,我将迈克尔的工作示例扩展如下:

Sub XmlText()

    Dim xml_namespace_uri As String
    Dim xml_document As Object
    Dim docnode01 As Object
    Dim docnode02 As Object

    xml_namespace_uri = "urn:iso:std:iso:20022:tech:xsd:pain.008.001.02"
    
    Set xml_document = CreateObject("MSXML2.DOMDocument.6.0")
    xml_document.setProperty "SelectionNamespaces", "xmlns:doc='" & xml_namespace_uri & "'"
    
    xml_document.async = False
    xml_document.validateOnParse = True
    xml_document.LoadXML _
        "<?xml version=""1.0"" encoding=""UTF-8""?>" & _
        "<Document xmlns:doc=""" & xml_namespace_uri & """ " & _
        "xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""/>"

    Set docnode01 = xml_document.SelectSingleNode("/Document")
    docnode01.appendChild _
        xml_document.createNode(1, "CstmrDrctDbtInitn", "xmlns:doc='" & xml_namespace_uri & "'")

    Set docnode02 = xml_document.SelectSingleNode("/Document/CstmrDrctDbtInitn")
    docnode02.appendChild _
        xml_document.createNode(1, "GrpHdr", "xmlns:doc='" & xml_namespace_uri & "'")

    Debug.Print xml_document.XML

End Sub

现在,程序在第二个 appendChild 语句中得到一个小插曲,并产生错误消息:“执行时出现错误 91:尚未设置对象变量或块变量”(免费翻译自荷兰语)。

这可能是“Dim As Object”语句,不够具体吗?我尝试了其他几种数据类型,都产生了一些错误消息。

2020-09-28 12:10:00

顺便说一句,添加以下语句也不能解决此问题:

    xml_document.setProperty "SelectionLanguage", "XPath"

2020-09-28 13:05

在回答 Parfait 的问题时,我尝试遵循“https://stackoverflow.com/questions/58026296/why-does-parsing-xml-document-using-msxml-v3-0-work-but -msxml-v6-0-doesnt”。我在 XPath 表达式中添加了命名空间前缀,如下所示:

    Set docnode01 = xml_document.SelectSingleNode("/doc:Document")

但随后下一个 appendChild 语句失败:“未设置对象变量”。抱歉,我可能没有完全理解我在做什么,以下尝试也失败了:

    Set docnode01 = xml_document.SelectSingleNode("/xmlns:doc:Document")
4

1 回答 1

1

正如@MichaelKay 所暗示的那样,问题似乎确实是MSXML 版本之间的命名空间处理。

用前缀设置SelectionNamespacesxml_doc.property 对我有用:xmlns:doc

Sub XmlText()
    xml_namespace_uri = "urn:iso:std:iso:20022:tech:xsd:pain.008.001.02"
    
    Set xml_document = CreateObject("MSXML2.DOMDocument.6.0")
    xml_document.SetProperty "SelectionNamespaces", "xmlns:doc='" & xml_namespace_uri & "'"
    
    xml_document.async = False
    xml_document.validateOnParse = True
    xml_document.LoadXML _
        "<?xml version=""1.0"" encoding=""UTF-8""?>" & _
        "<Document xmlns:doc=""" & xml_namespace_uri & """ " & _
        "xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""/>"
    
    Set docNode = xml_document.SelectSingleNode("/Document")
    docNode.appendChild _
        xml_document.createNode(1, "CstmrDrctDbtInitn", "xmlns:doc='" & xml_namespace_uri & "'")
    MsgBox (docNode.XML)
End Sub

在消息框中显示:

<Document xmlns:doc="urn:iso:std:iso:20022:tech:xsd:pain.008.001.02" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
  <CstmrDrctDbtInitn xmlns="xmlns:doc='urn:iso:std:iso:20022:tech:xsd:pain.008.001.02'"/>
</Document>
于 2020-09-24T19:19:54.317 回答