问题
I'm having trouble figuring out why my code isn't recognizing the tagname "HourlySchedule". When it gets to: For Each HourlySchedule In Resp.getElementsByTagName("HourlySchedule")
, it will skip to the end instead of looping thru each tag. I've tried several different tag names and it doesn't seem to work. Any suggestions?
My VBA code:
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
Here is the xml i'm trying to parse:
<?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:
An often cited issue in parsing XML documents is an undeclared namespace prefix which your response contains twice at different node levels. Notice no colon separated name is included which is a perfectly valid XML:
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<QueryResponse xmlns="http://markets.midwestiso.org/dart/xml">
As a result, in VBA declare such namespaces by designating user-defined prefixes, here doc and doc2 is used. Then, using SelectNodes
method over getElementsByTagName
since you will need to reference the second defined prefix as HourlySchedule is a child of Query node, you can then query to the needed element:
...
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
Output (in Immediate Window)
test
test
来源:https://stackoverflow.com/questions/44194763/getelementsbytagname-for-xml-response-text-not-working