none
How to Print an Empty XML Node using VBA RRS feed

  • Question

  • I am parsing an XML in VBA and printing each node along with its value in Excel. But the code only prints the nodes that have any value and leaves the ones without it.

    This is my code:

    Sub IterateThruElements()
        Dim xmlDoc      As MSXML2.DOMDocument60
        Dim xmlNodeList As MSXML2.IXMLDOMNodeList
        Dim xmlNode     As MSXML2.IXMLDOMNode
        Dim myNode      As MSXML2.IXMLDOMNode
    
        ' Create an Instance of the DOMDocument
        Set xmlDoc = New MSXML2.DOMDocument60
        xmlDoc.async = False
    
        ' Load XML information from a file. In this case, Cell C3 of Sheet "XML & DB2 Data" should contain the path of the XML
        strPathToXMLFile = Sheets("Credentials").Range("C3").Value
        xmlDoc.Load (strPathToXMLFile)
    
        ' Find out the number of child nodes in the file
        Set xmlNodeList = xmlDoc.getElementsByTagName("*")
    
        ' Open a new workbook and paste the data
        Sheets.Add
        ActiveSheet.Name = "TempSheet_1"
        Range("A1:B1").Formula = Array("Element Name", "Text")
        
        For Each xmlNode In xmlNodeList
        
            For Each myNode In xmlNode.ChildNodes
                If myNode.NodeType = NODE_TEXT Then
                    ActiveCell.Offset(0, 0).Formula = xmlNode.nodeName
                    ActiveCell.Offset(0, 1).Formula = xmlNode.Text
                End If
            Next myNode
    
            ActiveCell.Offset(1, 0).Select
    
        Next xmlNode
    
    End Sub
    
    
    Is there any way to print those empty nodes ?

    Friday, August 17, 2018 11:57 AM