我正在调用stockquote webservice并尝试解析返回的数据,但我尝试的任何工作都没有用
Set objHTTP = CreateObject("Microsoft.XMLHTTP")
objHTTP.Open "GET", "http://www.webservicex.net/stockquote.asmx/GetQuote?symbol=AAPL", False
objHTTP.send
Dim xDoc As MSXML2.DOMDocument
Dim xDoc2 As MSXML2.IXMLDOMNodeList
Set xDoc = New MSXML2.DOMDocument
xDoc.LoadXML (objHTTP.responseXML.XML)
我知道到目前为止一切正常,我可以查看调试器中的Xdoc对象,看看xml是否已加载。
如何在此之后访问各个节点?
示例xml看起来像这样
"<string xmlns="http://www.webserviceX.NET/"><StockQuotes><Stock><Symbol>AAPL</Symbol><Last>446.5345</Last>
<Date>5/30/2013</Date><Time>10:55am</Time><Change>+1.5845</Change></Stock></StockQuotes></string>"
答案 0 :(得分:3)
响应似乎是一个XML文档,其中包含一个包含转义XML文档的单个根元素:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://www.webserviceX.NET/"><StockQuotes><Stock><Symbol>AAPL</Symbol><Last>448.9501</Last><Date>5/30/2013</Date><Time>12:17pm</Time><Change>+4.0001</Change><Open>445.65</Open><High>449.77</High><Low>444.51</Low><Volume>5691335</Volume><MktCap>421.4B</MktCap><PreviousClose>444.95</PreviousClose><PercentageChange>+0.90%</PercentageChange><AnnRange>385.10 - 705.07</AnnRange><Earns>41.896</Earns><P-E>10.62</P-E><Name>Apple Inc.</Name></Stock></StockQuotes></string>
因此,您需要加载第一个文档,然后访问其内容并使用第二个文档将其解析为XML:
Dim doc1 As MSXML2.DOMDocument60
Set doc1 = New MSXML2.DOMDocument60
doc1.async = False
If doc1.load("http://www.webservicex.net/stockquote.asmx/GetQuote?symbol=AAPL") Then
Dim doc2 As MSXML2.DOMDocument60
Set doc2 = New MSXML2.DOMDocument60
If doc2.loadXML(doc1.DocumentElement.text) Then
Dim value
value = doc2.selectSingleNode("//Last").text
Else
'handle doc2.parseError here
End If
Else
' handle doc1.parseError her
End If
未经测试但应该给你一个想法。