SOAP中带有xpath的VBA DOM SelectSingleNode

时间:2018-08-26 22:16:14

标签: xml vba dom xpath

这是我的XML文件:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.cisco.com/AXL/API/10.5">
   <soapenv:Header/>
   <soapenv:Body>
      <ns:listTransPattern>
         <searchCriteria>
            <pattern>%</pattern>
            <routePartitionName>%</routePartitionName>
            <description>%</description>
         </searchCriteria>
         <returnedTags>
            <pattern>?</pattern>
            <routePartitionName>?</routePartitionName>
            <description>?</description>
         </returnedTags>
      </ns:listTransPattern>
   </soapenv:Body>
</soapenv:Envelope>

我尝试添加元素 <returnedTags>并行。如何在VBA中实施?非常感谢你!

1 个答案:

答案 0 :(得分:0)

如下所示。

如果要使用XPath表达式选择该元素,则必须首先使用.setProperty "SelectionLanguage"设置适当的选择名称空间。

Option Explicit
Sub test()
    Dim xTEST As New MSXML2.DOMDocument60

    With xTEST
        .validateOnParse = True
        .setProperty "SelectionLanguage", "XPath"
        .async = False
        .setProperty "SelectionNamespaces", "xmlns:ns=""http://www.cisco.com/AXL/API/10.5"""
        If Not .Load("C:\Test.xml") Then
            Err.Raise .parseError.ErrorCode, , .parseError.reason
        End If

        Dim a As IXMLDOMElement
        Set a = .SelectSingleNode("//ns:listTransPattern").appendChild(.createElement("Status"))

    End With
End Sub