I've already spent two weeks searching unsuccessfully how to parse one specific XML and fetch just few values. I already tried every single code on internet until I found one that solved part of my problem.
The XML i'm trying to fetch it's from U.S Department of Agriculture, and is free to access.
https://apps.fas.usda.gov/psdonline/app/index.html#/app/about
Dim xmlDoc As MSXML2.DOMDocument60
Dim xmlNode As MSXML2.IXMLDOMNode
Dim xmlNodeList As MSXML2.IXMLDOMNodeList
Dim myNode As MSXML2.IXMLDOMNode
Dim URL As String, APIkey As String
APIkey = "8DB688F8-1E22-4031-B581-59C221ECDDA6"
URL = "https://apps.fas.usda.gov/PSDOnlineDataServices/api/CommodityData/GetCommodityDataByYear?commodityCode=2222000&marketYear=2018"
Set xmlDoc = New MSXML2.DOMDocument60
xmlDoc.async = False
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", URL, False
.SetRequestHeader "Accept", "text/xml"
.SetRequestHeader "API_KEY", APIkey
.Send
xmlDoc.loadXML .ResponseText
End With
Set xmlNodeList = xmlDoc.getElementsByTagName("*")
For Each xmlNode In xmlNodeList
For Each myNode In xmlNode.childNodes
If myNode.nodeType = NODE_TEXT Then
Debug.Print xmlNode.nodeName & "=" & xmlNode.text
End If
Next myNode
Next xmlNode
Set xmlDoc = Nothing
End Sub
The response of this code show the entire XML listed, but when I try to find one specific node, the code result it's nothing.
in
Set xmlNodeList = xmlDoc.getElementsByTagName("*")
I've tried to use the address "//AttributeDescription", but apparently just work using the "*".
I need to receive, for example, The response below:
AttributeDescription=Production
CountryName=Brazil
Value=0.00000
I did my best trying to get the right response and I also consider that the XML structure it's not in the right format due the lack of response when addressing...
Is there anything that I can do to solve this issue?