I'm trying to teach myself the basics of VBA and XML to solve a specific problem I'm working on in Excel. I'm trying to use an API to pull census block IDs given lat/lon.
Here's the XML code:
<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>
The only VBA command I can get to work is
blockID = Doc.getElementsByTagName("Block")(0).innerText
which gives me the value with lots of html code still attached, as in
<Block FIPS="120950170151016">
Searching around, it seems like I might need to use the getAttributes function(?) but that doesn't appear to be an option in the dropdown as I'm using VBA. Which makes me wonder if I don't have one of the reference packages installed.
Any insights?
Edit: Thanks for insights below. I've tried using Load() instead of LoadXML() but it still doesn't seem to read any information into the object. The error is "The data necessary to complete this operation is not yet available" and it happens when the code tries to loop through the x values. Clear from the Locals viewer that there's no data in the object. Here's what I've got:
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