微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Xpath适用于MSXML2.DOMDocument,但不适用于MSXML2.DOMDocument.6.0

如何解决Xpath适用于MSXML2.DOMDocument,但不适用于MSXML2.DOMDocument.6.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,"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语句处进行了hickup,并生成错误消息:“执行时出错91:对象变量或块变量With尚未设置”(免费翻译自荷兰语)。

这可能是“ 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")

解决方法

@MichaelKay暗示,问题似乎是MSXML版本之间的名称空间处理。

import io b = io.BytesIO() with open("file.dat","br") as f: b.write(f.read()) b.seek(0) with open("new_file.dat","bw") as f: f.write(b.read()) b.close() 设置xml_doc.property前缀可以帮我解决问题:SelectionNamespaces

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。