首页 > 解决方案 > Excel VBA - 如何从 XML URL 获取数据?

问题描述

我需要从 FIELD NAME="sitr2" & FIELD NAME="sitr4" 获取数据。下面是 XML 代码:

<SEGMENTS>
<SEGMENT NAME="webcluster">
<RESULTPAGE>
<QUERYTRANSFORMS>...</QUERYTRANSFORMS>
<NAVIGATION ENTRIES="0"> </NAVIGATION>
<CLUSTERS/>
<RESULTSET FIRSTHIT="1" LASTHIT="1" HITS="1" TOTALHITS="1" MAXRANK="10000" TIME="0.5673">
<HIT NO="1" RANK="10000" SITEID="0" MOREHITS="0" FCOCOUNT="0">
<FIELD NAME="rank">10000</FIELD>
<FIELD NAME="personnames"/>
<FIELD NAME="concepts">environment;flexible;storage;virtual;growing</FIELD>
<FIELD NAME="companyteaser"/>
<FIELD NAME="locationteaser"/>
<FIELD NAME="personnameteaser"/>
<FIELD NAME="nav">...</FIELD>
<FIELD NAME="taxonomy">t/na</FIELD>
<FIELD NAME="documentid"/>
<FIELD NAME="uniqueid"/>
<FIELD NAME="sitr1">4038b5c5-c90b-4ae3-a165-a5e0db9c10d4</FIELD>
<FIELD NAME="sitr2">097cf4a8-2755-4c62-939c-9402e0a4e3e2</FIELD>
<FIELD NAME="sitr3">Unknown</FIELD>
<FIELD NAME="sitr4">0.3</FIELD>
<FIELD NAME="sitr5"/>
<FIELD NAME="sitr8"><matches> </matches></FIELD>
<FIELD NAME="int1">21854</FIELD>

我不知道从哪里开始。我需要提取并插入到excel中。希望有人可以帮助我。

标签: xmlvbaexcel

解决方案


您可以使用 Internet Explorer。如何在 VBA 中使用 IE 的起始页在这里:wiseowl

在您的情况下,代码将是:

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub main()
Dim ie As Object
Application.ScreenUpdating = True
Set ie = CreateObject("internetexplorer.application")
With ie
.Visible = True
.navigate "C:\Temp\some.xml"
Do While ie.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
End With
Sleep 300
With ie.document.all.tags("FIELD")
For i = 0 To .Length - 1
Cells(i + 1, 1) = .Item(i).getAttribute("NAME")
Next i
End With
ie.Quit
Set ie = Nothing
End Sub

推荐阅读