我正在使用MSXML2加载XML文件(来自Google Analytics的Feed)。
我现在需要从中获取数据,但我无法弄清楚如何。
这是XML:
<?xml version='1.0' encoding='utf-8'?>
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:dxp='http://schemas.google.com/analytics/2009' xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/' xmlns:gd='http://schemas.google.com/g/2005' gd:etag='W/"dsdsdsdsdsdsdsd."' gd:kind='analytics#data'>
<id>http://www.google.com/analytics/feeds/data?ids=ga:2000000&dimensions=ga:campaign&metrics=ga:visits&filters=ga:campaign%3D%3Dcrosspromo&start-date=2010-08-26&end-date=2010-09-09</id>
<updated>2010-09-09T05:50:40.705-07:00</updated>
<title>Google Analytics Data for Profile 2000000</title>
<link rel='self' type='application/atom+xml' href='https://www.google.com/analytics/feeds/data?max-results=50&end-date=2010-09-09&start-date=2010-08-26&metrics=ga%3Avisits&ids=ga%3A2000000&dimensions=ga%3Acampaign&filters=ga%3Acampaign%3D%3Dcrosspromo' />
<author>
<name>Google Analytics</name>
</author>
<generator version='1.0'>Google Analytics</generator>
<openSearch:totalResults>1<
/openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>50</openSearch:itemsPerPage>
<dxp:aggregates>
<dxp:metric confidenceInterval='0.0' name='ga:visits' type='integer' value='5070' />
</dxp:aggregates>
<dxp:dataSource>
<dxp:property name='ga:profileId' value='2000000' />
<dxp:property name='ga:webPropertyId' value='UA-2000000-1' />
<dxp:property name='ga:accountName' value='test' />
<dxp:tableId>ga:2000000</dxp:tableId>
<dxp:tableName>test</dxp:tableName>
</dxp:dataSource>
<dxp:endDate>2010-09-09</dxp:endDate>
<dxp:startDate>2010-08-26</dxp:startDate>
<entry gd:etag='W/"CkMEQX47eSp7I2A9Wx5QGUQ."' gd:kind='analytics#datarow'>
<id>http://www.google.com/analytics/feeds/data?ids=ga:2000000&ga:campaign=crosspromo&filters=ga:campaign%3D%3Dcrosspromo&start-date=2010-08-26&end-date=2010-09-09</id>
<updated>2010-09-08T17:00:00.001-07:00</updated>
<title>ga:campaign=crosspromo</title>
<link rel='alternate' type='text/html' href='http://www.google.com/analytics' />
<dxp:dimension name='ga:campaign' value='crosspromo' />
<dxp:metric confidenceInterval='0.0' name='ga:visits' type='integer' value='5070' />
</entry></openSearch:totalResults>
</feed>
然后我加载XML并搜索“entry”标签:
Dim objXML As MSXML2.DOMDocument
Set objXML = New MSXML2.DOMDocument
objXML.loadXML strXML
Debug.Print objXML.getElementsByTagName("entry").Item(0).Text
但是这会输出:
http://www.google.com/analytics/feeds/data?ids=ga:20000000&ga:campaign=crosspromo&filters=ga:campaign%3D%3Dcrosspromo&start-date=2010-08-26&end-date=2010-09-092010-09-08T17:00:00.001-07:00ga:campaign=crosspromo
其他数据在哪里? 如果我尝试第(1)项就失败了......
答案 0 :(得分:4)
Well Item(1)失败,因为只有一个“entry”元素,而且它是0。
.Text属性返回元素及其所有子元素的所有Text内容。如果仔细观察,那就是你所拥有的。
如果你想要别的东西,你必须在Item(0)之外使用适当的方法。
查看此问题(How to parse XML in VBA)。
答案 1 :(得分:4)
尝试:
Option Explicit
Public Function LoadXmlDoc() As DOMDocument
Dim aDoc As DOMDocument
Set aDoc = New DOMDocument
aDoc.async = False
aDoc.Load ("feedfile.xml")
Set LoadXmlDoc = aDoc
End Function
'' Direct access
Public Sub getEntry()
Dim aDoc As DOMDocument
Dim aNode As IXMLDOMNode
Set aDoc = LoadXmlDoc
Set aNode = aDoc.SelectSingleNode("//entry")
Debug.Print aNode.XML
End Sub
'' search for it
Public Sub findEntry()
Dim aDoc As DOMDocument
Dim aNode As IXMLDOMNode
Dim aNodes As IXMLDOMNodeList
Set aDoc = LoadXmlDoc
Set aNodes = aDoc.getElementsByTagName("entry")
For Each aNode In aNodes
Debug.Print aNode.XML
Next
End Sub
'' get the entry date
Public Sub getDate()
Dim aDoc As DOMDocument
Dim aNode As IXMLDOMNode
Set aDoc = LoadXmlDoc
Set aNode = aDoc.SelectSingleNode("//entry/updated")
Debug.Print aNode.Text
End Sub
答案 2 :(得分:0)
您已收到所要求的所有信息。您获得了<entry></entry>
让我们分解(这是你的输出):
第一组文字:
http://www.google.com/analytics/feeds/data?ids=ga:20000000&ga:campaign=crosspromo&filters=ga:campaign%3D%3Dcrosspromo&start-date=2010-08-26&end-date=2010-09-09
第二组文字:
2010-09-08T17:00:00.001-07:00
第三组文字:
ga:campaign=crosspromo
输入中没有其他内容。因此,您没有看到任何其他内容。