我想教自己的VBA和XML的基础知识来解决我在Excel中工作的特定问题。我正在尝试使用API来提取经纬度给定的人口普查区块ID。试图用VBA来解析html
这里的XML代码:
<Response xmlns="http://data.fcc.gov/api" status="OK" executionTime="120">
<Block FIPS="120950170151016">
<intersection FIPS="120950170151016"></intersection>
<intersection FIPS="120950170151019"></intersection>
<intersection FIPS="120950170151015"></intersection>
</Block>
<County FIPS="12095" name="Orange"></County>
<State FIPS="12" code="FL" name="Florida"></State>
<messages>FCC0001: The coordinate lies on the boundary of mulitple blocks, first FIPS is displayed. For a complete list use showall=true to display 'intersection' element in the Block</messages>
<head></head>
</Response>
唯一的VBA命令我能得到的工作是
blockID = Doc.getElementsByTagName("Block")(0).innerText
这使我有很多的HTML代码仍然附加价值,如
<Block FIPS="120950170151016">
搜索周围,似乎我可能需要使用getAttributes函数(?),但不在我使用VBA时,似乎是下拉菜单中的一个选项。这让我怀疑我是否没有安装任何参考软件包。
任何见解?
编辑:感谢下面的见解。我已经尝试使用Load()而不是LoadXML(),但它仍然没有向对象中读取任何信息。错误是“完成此操作所需的数据尚不可用”,并且在代码尝试循环访问x值时发生。从本地浏览器清除对象中没有数据。下面是我得到了什么:
Dim oAttribute, item
Dim x As Long
Dim apiURLstub, apiURL As String
apiURLstub = "http://data.fcc.gov/api/block/find?"
'append lat/lon info to URL
Dim lat As Double
Dim lon As Double
lat = Range("A3").Value
lon = Range("B3").Value
apiURL = apiURLstub & "latitude=" & lat & "&longitude=" & lon & "&showall=true"
Dim objXML As Object, node As Object
Set objXML = New MSXML2.DOMDocument
If Not objXML.Load(apiURL) Then 'strXML is the string with XML'
Err.Raise objXML.parseError.ErrorCode, , objXML.parseError.reason
Else
Set node = objXML.getElementsByTagName("intersection")
For x = 0 To node.Length - 1
For Each oAttribute In node(x).Attributes
Debug.Print oAttribute.Value
Next
Next
End If
XML!= HTML。改为使用[Microsoft.XMLDOM](https://msdn.microsoft.com/en-us/library/aa468547.aspx)。 – Comintern
您是否尝试过使用[Regex](http://stackoverflow.com/a/1732454/1641172)? –
对。 XML。感谢您回答我的愚蠢问题! – jer312