Dr. Demento
Well-known Member
- Joined
- Nov 2, 2010
- Messages
- 618
- Office Version
- 2019
- 2016
- Platform
- Windows
I'm attempting to "load" an .xml string into VBA to determine the properties of a cell. The code:
provides an .xml output of various properties of the activecell. I have attempted to use the following code, but it fails to load
I know I could actually download the .xml file to my computer, but my work settings prohibit me from opening the file again, so I've got to load the xml data directly into VBA. I know this code works on other files, but I'm stuck getting it to work for the .Value(11) output.
I realize this code would fail in Reading the .xml file (it's specific to the test file listed above), but I'm more concerned with getting the data loaded at this point.
Thanks y'all for your help.
VBA Code:
?ActiveCell.Value(11)
VBA Code:
Sub TestXML()
' https://analystcave.com/vba-xml-working-xml-files/
' https://excel-macro.tutorialhorizon.com/vba-excel-read-xml-by-looping-through-nodes/
Dim xmlSrc As Object, root As Object
Set xmlSrc = CreateObject("MSXML2.DOMDocument")
xmlSrc.Async = False: xmlSrc.validateOnParse = False
xmlSrc.Load (ActiveCell.Value(11)) '("C:\Desktop\test.xml") ' <<--- Fails here. The test file is found here:[B] https://msdn.microsoft.com/en-us/library/ms762271%28v=vs.85%29.aspx[/B]
Set root = xmlSrc.DocumentElement
'Get Document Elements
Set Lists = xmlSrc.DocumentElement
'Get first child ( same as ChildNodes(0) )
Set getFirstChild = Lists.FirstChild
'Print first child XML
Debug.Print getFirstChild.XML
'Print first child Text
Debug.Print getFirstChild.text
Set Books = xmlSrc.SelectNodes(" / catalog / book")
For i = 0 To Books.Length - 1
For j = 0 To Books(i).ChildNodes.Length - 1
Debug.Print Books(i).ChildNodes(j).tagName
Debug.Print Books(i).ChildNodes(j).text
Next
Next
Set xmlSrc = Nothing
'Select the tag from the XML file using SelectNodes or SelectSingleNode. _
SelectNodes – Selects a list of nodes matches the Xpath pattern.
'' Set Books = oXMLFile.SelectNodes(“ / catalog / book”)
End Sub
I know I could actually download the .xml file to my computer, but my work settings prohibit me from opening the file again, so I've got to load the xml data directly into VBA. I know this code works on other files, but I'm stuck getting it to work for the .Value(11) output.
I realize this code would fail in Reading the .xml file (it's specific to the test file listed above), but I'm more concerned with getting the data loaded at this point.
Thanks y'all for your help.