Delete a node in XML

Posted on 2012-08-27
Last Modified: 2012-09-12
I have a situation where a node has to be deleted from XML file.I am trying to delete the below node from the attached.Any suggestions.

<ac:time status="63">
Question by:Govinda2020
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    What language are you using?
    LVL 18

    Expert Comment

    Here's a sample, how to select and delete a node using VBScript
    You need to correct it, placing the right "ac" namespace definition in the line 15
    I reads the source from a file named ac.xml

    Option Explicit
    Dim xml
    set xml = CreateObject("Msxml2.DOMDocument.6.0")
    xml.async = False
    xml.setProperty "ProhibitDTD", false
    ' 1. read the XML
    if xml.parseError.errorCode <> 0 then
       WScript.Echo "Error: " & xml.parseError.errorCode & " Reason: " & xml.parseError.reason
    end if
    call xml.setProperty("SelectionLanguage", "XPath")
    call xml.setProperty("SelectionNamespaces", "xmlns:ac='ac'")
    ' 2. Delete node
    dim e, nm
    set e = xml.documentElement.selectSingleNode("//ac:time[@status='63']")
    if not e is Nothing then e.parentNode.removeChild( e )
    WScript.Echo xml.xml

    Open in new window


    Author Comment

    Thanks ZC2 , I will try this and let you know. But the issue I have there are two nodes with the same name i.e ac:integer
    LVL 18

    Accepted Solution

    If you need to delete only one of them, then you have to correct the XPath expression (the string argument which is passed to the selectSingleNode() call) that way so the correct node will be found.
    If you need to delete both of them (or more), instead of the selectSingleNode() call use selectNodes() and then process each of the list of found nodes the same way as single node.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
    Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    760 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

    7 Experts available now in Live!

    Get 1:1 Help Now