I'm struggling to parse an XML file. I found many examples, but none seem to be exactly what I am looking for, and I cannot get through the error "Object variable or variable block not set"
xml is well formed and looks like this:
<xml tag> <PLMXML> <WorkflowTemplate name=""> <argument name=""> </argument> </WorkflowTemplate > <WorkflowTemplate name="">
etc..
I am trying to use VBA to get the value of all the names of children individually and get the names of the arguments. I keep getting the error with this code:
Dim xmlDoc As MSXML2.DOMDocument Dim xmlElement As MSXML2.IXMLDOMElement Dim xmlNode As MSXML2.IXMLDOMNode Dim xmlAttribute As MSXML2.IXMLDOMAttribute Set xmlDoc = New MSXML2.DOMDocument xmlDoc.async = False xmlDoc.validateOnParse = False 'ENTER THE PATH WHERE THE XML Workflow DOCUMENT IS STORED: Dim DocumentPath As String DocumentPath = InputBox("Enter the full path for the xml workflow document, example: C:\workflows\workflowseasy.xml", "Workflow XML File path", "C:\workflows\workflowseasy.xml") xmlDoc.Load (DocumentPath) Set xmlElement = xmlDoc.DocumentElement Set xmlNode = xmlElement.SelectSingleNode("WorkflowTemplate[0]") Set xmlAtribute = xmlNode.Attributes.getNamedItem("name")
I do not understand how to get to the data in the document using this parser in excel vba. Any help would be greatly appreciated. I currently have Microsoft XML, v6.0, selected in links.
UPDATE
I already burst into it and came up with the following code, although I still get the same error:
Dim xmlDoc As MSXML2.DOMDocument60 Dim xmlRoot As MSXML2.IXMLDOMNode Dim xmlTemplate As MSXML2.IXMLDOMNode Dim xmlAttributes As MSXML2.IXMLDOMNamedNodeMap Dim xmlName As MSXML2.IXMLDOMNode Dim xmlChildren As MSXML2.IXMLDOMNodeList Dim xmlChild As MSXML2.IXMLDOMNode Dim intI As Long intI = 1 Set xmlDoc = New MSXML2.DOMDocument60 xmlDoc.async = False xmlDoc.validateOnParse = False 'ENTER THE PATH WHERE THE XML Workflow DOCUMENT IS STORED: Dim DocumentPath As String DocumentPath = InputBox("Enter the full path for the xml workflow document, example: C:\workflows\workflowseasy.xml", "Workflow XML File path", "C:\workflows\workflowseasy.xml") xmlDoc.Load (DocumentPath) Set xmlRoot = xmlDoc.DocumentElement *****these say they are empty when debugging Set xmlChildren = xmlRoot.ChildNodes *****these say they are empty when debugging For Each xmlTemplate In xmlChildren *****error occures here If xmlTemplate.nodeName = "WorkflowTemplate" Then Set xmlAttributes = xmlTemplate.Attributes Set xmlName = xmlAttributes.getNamedItem("name") ActiveSheet.Cells(int1, 1).Value = xmlName.Text Set xmlChildren = xmlTemplate.ChildNodes intI = intI + 1 End If Next xmlTemplate
FINAL UPDATE * *
Figured it out. Problem downloading file. For some reason, passing this line from the msg field does not work, but passing it from the file selector gui does. Here is the code I used.
Dim xmlDoc As MSXML2.DOMDocument60 Dim xmlRoot As MSXML2.IXMLDOMNode Dim xmlTemplate As MSXML2.IXMLDOMNode Dim xmlAttributes As MSXML2.IXMLDOMNamedNodeMap Dim xmlName As MSXML2.IXMLDOMNode Dim xmlChildren As MSXML2.IXMLDOMNodeList Dim xmlChild As MSXML2.IXMLDOMNode Dim intI As Long intI = 1 Set xmlDoc = New MSXML2.DOMDocument60 xmlDoc.async = False xmlDoc.validateOnParse = False 'ENTER THE PATH WHERE THE XML Workflow DOCUMENT IS STORED: Dim DocumentPath As String With Application.FileDialog(msoFileDialogOpen) .Title = "Choose File" .AllowMultiSelect = False .Show 'DocumentPath.Show DocumentPath = .SelectedItems(1) End With xmlDoc.Load (DocumentPath) Set xmlRoot = xmlDoc.DocumentElement Set xmlChildren = xmlRoot.ChildNodes For Each xmlTemplate In xmlChildren If xmlTemplate.nodeName = "WorkflowTemplate" Then Set xmlAttributes = xmlTemplate.Attributes Set xmlName = xmlAttributes.getNamedItem("name") ActiveSheet.Cells(int1, 1).Value = xmlName.Text Set xmlChildren = xmlTemplate.ChildNodes intI = intI + 1 End If Next xmlTemplate
Currently, the code is broken down into a value assignment section, but passing through the code, the variables pull the correct values ββand correctly extract the xml information.