Search code examples
excelxmlvba

Removing a XML child node with VBA


This is the VBA code I'm currently using to import an XML into my workbook (which works just fine):

Sub matomoImportXML()

Dim matomoXML As Workbook

Application.DisplayAlerts = False
Set matomoXML = Workbooks.OpenXML(FileName:=matomo_xml, LoadOption:=xlXmlLoadImportToList)
Application.DisplayAlerts = True

matomoXML.Sheets(1).UsedRange.Copy report.Sheets(matomo_data).Range("A1")
matomoXML.Close False

End Sub

And this is an example of the XML file:

<?xml version="1.0" encoding="UTF-8"?>
<result>
    <row>
        <label>lp_total_pageviews=1 - my-website.com/please-verify-you-age-to-enter/ - Others</label>
        <nb_uniq_events_eventaction>118</nb_uniq_events_eventaction>
        <nb_uniq_events_eventcategory>118</nb_uniq_events_eventcategory>
        <nb_uniq_corehome_visitip>118</nb_uniq_corehome_visitip>
        <level>3</level>
        <Events_EventAction>lp_total_pageviews=1</Events_EventAction>
        <Events_EventCategory>my-website.com%2Fplease-verify-you-age-to-enter%2F</Events_EventCategory>
        <is_summary>1</is_summary>
        <CoreHome_VisitIp>Others</CoreHome_VisitIp>
    </row>
    <row>
        <label>lp_total_clicks=1 - my-website.com/please-verify-you-age-to-enter-rs/ - xxxx:1009:b00a:6fd8:d937:5eb2:7563:de56</label>
        <nb_uniq_events_eventaction>3</nb_uniq_events_eventaction>
        <nb_uniq_events_eventcategory>3</nb_uniq_events_eventcategory>
        <nb_uniq_corehome_visitip>3</nb_uniq_corehome_visitip>
        <level>3</level>
        <Events_EventAction>lp_total_clicks=1</Events_EventAction>
        <Events_EventCategory>my-website.com%2Fplease-verify-you-age-to-enter-rs%2F</Events_EventCategory>
        <CoreHome_VisitIp>XXXX:1009:b00a:6fd8:d937:5eb2:7563:de56</CoreHome_VisitIp>
    </row>
</result>

Now, before I import and copy it to my workbook, I have to loop through the XML and delete the child node <is_summary>some value</is_summary> completely (if it exist).

I've been trying out multiple solutions I found online, but so far unsuccessful.

Any help would be greatly appreciated!


Solution

  • Please, try the next way:

    Sub testRemoveXMLNode()
       'it needs a reference to 'Microsoft XML, v6.0'
       Dim xmlPath As String, XDoc As MSXML2.DOMDocument60, n As MSXML2.IXMLDOMNode
       
       xmlPath = "Your XML file full name" 'use the real name
       Set XDoc = New MSXML2.DOMDocument60
       XDoc.Load (xmlPath)
       Debug.Print XDoc.XML & vbCrLf & "___"
        Dim strTag As String: strTag = "is_summary"
        
        For Each n In XDoc.DocumentElement.ChildNodes
            recursiveTagSrc n, strTag
        Next n
    
        Debug.Print XDoc.XML 'just to see the result
        'it can be saved as another XML document or overwrite the original XML file (xDoc.Save xmlPath)...
    End Sub
    Sub recursiveTagSrc(n As MSXML2.IXMLDOMNode, strTag As String)
        Dim Nd As MSXML2.IXMLDOMNode
        If n.HasChildNodes Then
            For Each Nd In n.ChildNodes
                If Nd.HasChildNodes Then recursiveTagSrc Nd, strTag
                If Nd.nodeName = strTag Then
                    n.RemoveChild Nd
                End If
            Next Nd
        End If
    End Sub