Linux 从 VBA 中的 XML 文档中获取信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3677031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:09:46  来源:igfitidea点击:

Getting info out of XML document in VBA

xmlms-accessvbamsxml

提问by skerit

I'm using MSXML2 to load an XML file (a feed from Google Analytics).

我正在使用 MSXML2 加载 XML 文件(来自 Google Analytics 的提要)。

I now need to get data out of it, but I can't figure out how.

我现在需要从中获取数据,但我不知道如何。

This is the XML:

这是 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&amp;dimensions=ga:campaign&amp;metrics=ga:visits&amp;filters=ga:campaign%3D%3Dcrosspromo&amp;start-date=2010-08-26&amp;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&amp;end-date=2010-09-09&amp;start-date=2010-08-26&amp;metrics=ga%3Avisits&amp;ids=ga%3A2000000&amp;dimensions=ga%3Acampaign&amp;filters=ga%3Acampaign%3D%3Dcrosspromo' />
  <author>
    <name>Google Analytics</name>
  </author>
  <generator version='1.0'>Google Analytics</generator>
  <openSearch:totalResults>1&lt;
  /openSearch:totalResults&gt;
  <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&amp;ga:campaign=crosspromo&amp;filters=ga:campaign%3D%3Dcrosspromo&amp;start-date=2010-08-26&amp;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>

Then I load the XML and search for the "entry" tag:

然后我加载 XML 并搜索“entry”标签:

Dim objXML As MSXML2.DOMDocument
Set objXML = New MSXML2.DOMDocument
objXML.loadXML strXML
Debug.Print objXML.getElementsByTagName("entry").Item(0).Text

But this outputs:

但这输出:

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

Where's the rest of the data? If I try item(1) it fails...

其余的数据在哪里?如果我尝试 item(1) 失败...

回答by Benjol

Well Item(1) fails because there is only one "entry" element, and that's at 0.

好吧,Item(1) 失败了,因为只有一个“条目”元素,而且是 0。

The .Text property returns all the Text content of the element and all its children. If you look closely, that's what you've got.

.Text 属性返回元素及其所有子元素的所有Text 内容。如果你仔细观察,这就是你所拥有的。

If you want something else, you'll have to use the appropriate method off the Item(0).

如果您想要其他东西,则必须使用 Item(0) 之外的适当方法。

Check out this question (How to parse XML in VBA).

查看这个问题(如何在 VBA 中解析 XML)。

回答by Scott's Oasys

You have received all the information you asked for. You got the text of each element within the <entry></entry>

您已收到您要求的所有信息。你得到了每个元素的文本<entry></entry>

lets break it down (this was your output):

让我们分解一下(这是你的输出):

First set of 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-09

Second Set of text:

第二组文字:

2010-09-08T17:00:00.001-07:00

Third set of text:

第三组文字:

ga:campaign=crosspromo

There was nothing else in the entry with output. Therefore you do not see anything else.

带有输出的条目中没有其他内容。因此,您看不到其他任何东西。

回答by Marc Thibault

Try:

尝试:

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