[Last Call] Learn how to a build a cloud-first strategyRegister Now


Delete a node in XML

Posted on 2012-08-27
Medium Priority
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
  • 2
LVL 75

Expert Comment

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

Expert Comment

ID: 38337228
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

ID: 38345850
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 19

Accepted Solution

zc2 earned 2000 total points
ID: 38346245
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.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview

830 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