Deleting all children of an XML node

Posted on 2005-04-27
Last Modified: 2012-05-05
Hi everyone,

I am using VBA for MS office. I would like to know how I can delete all children of a node. The method should not fail if the node has no children.

So, basically I have:

Dim xmlParser as Object
Set xmlParser = CreateObject("MSXML2.DOMDocument")
xmlParser.LoadXML (documentXMLData)
Dim serializedElment as Object
Set serializedElement = xmlParser.ChildNodes(0).selectSingleNode("SerializedData")

Now I want to delete all children (if any) of the serializedElement node.

How can I do this?


Question by:xargon_123
    LVL 10

    Expert Comment

    Private Sub delChildren(tvw As TreeView, nod As Node)
        If nod Is Nothing Then Exit Sub
        Do Until nod.Child Is Nothing
            tvw.Nodes.Remove nod.Child.Index
    End Sub
    LVL 10

    Expert Comment

    Whoops wrong answer
    LVL 10

    Accepted Solution

    Private Sub delChildren(element  As object)
        If element Is Nothing Then Exit Sub
        Do Until element.childNodes(0) Is Nothing
            element.removeChild element.childNodes(0)
    End Sub
    LVL 10

    Expert Comment

    And you would call it like this:

        delChildren serializedElement

    Is there any reason you are doing all this using late-binding. Early binding would be more efficient and you would get the added help of being able to use intellisense.

    Also I asked a question regarding a previous PAQ that you accepted from me and would appreciate an answer:

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now