检索XML属性值

时间:2015-07-30 17:00:13

标签: xml vba excel-vba excel

使用以下XML文档:

http://www.broadbandmap.gov/broadbandmap/census/block?latitude=40.784045&longitude=-73.845828&format=xml

我想从节点获取值"360050216022002"

<block FIPS="360050216022002">

这是VBA中的代码,但该数字不在节点中,因此我无法使用 SelectNodes

如何更改代码?

Function GetCensusTrack(lat As String, lon As String) As String

' Requires a reference to Microsoft XML, v6.0'


    Dim myRequest As XMLHTTP60
    Dim blockNode As IXMLDOMNode ' Can I set it as Node?'

    Dim uu As String

    Set myRequest = New XMLHTTP60

    uu = "http://www.broadbandmap.gov/broadbandmap/census/block?latitude=" & lat & "&longitude=" & lon & "&format=xml"
    myRequest.Open "GET", uu, False
    myRequest.send

   If myRequest.readyState = 4 Then
    Set myDomDoc = New DOMDocument60

   myDomDoc.LoadXML myRequest.responseText
' Get the latitude and longitude node values'

'我应该在这里输入什么?'         设置blockNode = ????       万一     结束功能

1 个答案:

答案 0 :(得分:1)

FIPS<block>的属性。您可以使用@指定要查询属性:

Dim a As Object
Set a = myDomDoc.SelectSingleNode("/Response/Results/block/@FIPS")

If Not a Is Nothing Then
    Debug.Print "Attribute value = " & a.Text
End If