need help with removeChild()
narendar rao <[email protected]> Wed, 18 Feb 2009 15:07:27 -0500
| Newsgroups | gmane.comp.python.xml |
|---|---|
| Message-ID | <[email protected]> |
Hi, I am newbie to Python and trying to edit a XML file. I am attaching the XML file. I am trying to remove the Node which has a certain attribute value. For example, I want to remove <c> Node after checking if the attribute value(Name) equals to the command line parameter passed. In the removeConfiguration, i want to iterate over the nodes and check for attribute value and then remove child. If we use removeChild(), will it save changes to the file that we are editing. If it doesn't, can some one show me how to do that. Can some one help me in writing this code? It will help me in understanding some of the things alot. Thanks, Narender _______________________________________________ XML-SIG maillist - [email protected] http://mail.python.org/mailman/listinfo/xml-sig
testxml.py
(text/plain, 856 B)
import os,sys
from xml.dom import minidom
def Usage():
print "\n Usage: '%s' Folder location Attribute value"
print "\n Example: '%s' G:\test write"
sys.exit(1)
def CheckArgs():
if len (sys.argv) < 3:
Usage()
sys.exit(-1)
def RemoveConfiguration(file,doc,sSrcAttribute):
node = doc.childNodes[0]
def callBack(ctx,root,files):
for file in files:
file = root + "\\" + file
if not file.lower().endswith('.xml'):
continue
try:
print file
doc = minidom.parse(file)
RemoveConfiguration(file,doc,u'Name')
except:
print "Error in Processing"
CheckArgs()
sSrcFolder = sys.argv[1]
sSrcAttribute = sys.argv[2]
os.path.walk(sSrcFolder,callBack,0)
xmlfile.xml
(text/xml, 119 B)
<A> <B> </B> <ba> </ba> <c Name = "any"> </c> <c Name = "file"> </c> <c Name = "write"> </c> <d> </d> </A>