getelementsbytagname用于xml响应文本无法正常工作

时间:2017-05-26 06:10:35

标签: xml vba api xml-documentation

我无法弄清楚为什么我的代码无法识别标记名" 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>

1 个答案:

答案 0 :(得分:0)

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

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

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

因此,在VBA中通过指定用户定义的前缀来声明此类命名空间,此处使用 doc doc2 。然后,在SelectNodes上使用getElementsByTagName方法,因为您需要引用第二个定义的前缀 HourlySchedule Query 节点的子节点,您可以然后查询所需的元素:

...
   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

输出 (在即时窗口中)

test
test