xml 响应文本的 getelementsbytagname 不起作用

getelementsbytagname for xml response text not working

我无法弄清楚为什么我的代码无法识别标记名 "HourlySchedule"。当它到达:For Each HourlySchedule In Resp.getElementsByTagName("HourlySchedule") 时,它将跳到末尾,而不是循环遍历每个标签。我尝试了几种不同的标签名称,但似乎都不起作用。有什么建议么?

我的VBA代码:

    Sub Button1_Click()
    Dim URL As String: URL = "webaddress here"
    Dim mfile As String
    mfile = "<?xml version=" & """" & "1.0" & """" & "?><Envelope xmlns=" & """" & "http://schemas.xmlsoap.org/soap/envelope/" & """" & "><Header/><Body><QueryRequest xmlns=" & """" & "http://markets.midwestiso.org/dar/xml" & """" & "><QueryMarketResults day=" & """" & "2017-03-05" & """" & "><LocationName>Rug</LocationName></QueryMarketResults></QueryRequest></Body></Envelope>"

    Set Req = New WinHttp.WinHttpRequest
    With Req
        .Open "POST", URL, False
        .SetClientCertificate "CURRENT_USER\MY\name"
        .SetRequestHeader "content-type", "text/xml"
        .Send (mfile)
        .WaitForResponse
    End With

    Dim Resp As New MSXML2.DOMDocument60
    Resp.LoadXML Req.ResponseText

if Resp.loadxml (Req.ResponseText) then
   MsgBox "ok" else
   MsgBox "err"
end if

    Dim HourlySchedule As IXMLDOMNode
    For Each HourlySchedule In Resp.getElementsByTagName("HourlySchedule") ''this is where my problem is
    Debug.Print "test"
    Next HourlySchedule
    End Sub

这是我要解析的 xml:

<?xml version="1.0" encoding="UTF-8"?>
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <QueryResponse xmlns="http://markets.midwestiso.org/dart/xml">
            <MarketResults day="2017-03-05">
                <Location name="OTP.RUGBY1_IBR">
                    <HourlySchedule hour="1">
                        <ClearedEnergy MW="-6" virtualMW="0" price="7" capped="false"/>
                        <ClearedReg MW="0" price="10.18" capped="false"/>
                        <ClearedSpin MW="0" price="1" capped="false"/>
                        <ClearedSupp MW="0" price="0.5" capped="false"/>
                        <ClearedRampCapabilityUp MW="0" price="0"/>
                        <ClearedRampCapabilityDown MW="0" price="0"/>
                    </HourlySchedule>
                    <HourlySchedule hour="2">
                        <ClearedEnergy MW="-2" virtualMW="0" price="5.3" capped="false"/>
                        <ClearedReg MW="0" price="8.06" capped="false"/>
                        <ClearedSpin MW="0" price="1" capped="false"/>
                        <ClearedSupp MW="0" price="0.5" capped="false"/>
                        <ClearedRampCapabilityUp MW="0" price="0"/>
                        <ClearedRampCapabilityDown MW="0" price="0"/>
                    </HourlySchedule>
                </Location>
            </MarketResults>
        </QueryResponse>
    </Body>
</Envelope>

解析 XML 文档时经常提到的一个问题是未声明的名称空间前缀,您的响应在不同的节点级别包含两次。请注意,没有包含冒号分隔的名称,这是完全有效的 XML:

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">

<QueryResponse xmlns="http://markets.midwestiso.org/dart/xml">

因此,在VBA中通过指定用户定义的前缀来声明此类命名空间,这里docdoc2是用过的。然后,在 getElementsByTagName 上使用 SelectNodes 方法,因为您需要引用第二个定义的前缀,因为 HourlyScheduleQuery[=27 的子项=]节点,然后可以查询到需要的元素:

...
   Dim HourlySchedule As IXMLDOMNode    
   Dim XmlNamespaces As String

   ' SPACE SEPARATED STRING
   XmlNamespaces = "xmlns:doc='http://schemas.xmlsoap.org/soap/envelope/'" _
                   & " xmlns:doc2='http://markets.midwestiso.org/dart/xml'"
   Resp.setProperty "SelectionNamespaces", XmlNamespaces

   For Each HourlySchedule In Resp.DocumentElement.SelectNodes("//doc2:HourlySchedule")
       Debug.Print "test"
   Next HourlySchedule

   Set Resp = Nothing 

End Sub

输出(立即Window)

test
test