need help with removeChild()

View: New views
3 Messages — Rating Filter:   Alert me  

need help with removeChild()

by narendar rao-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

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)

<A>
<B>
</B>
<ba>
</ba>
<c Name = "any">
</c>
<c Name = "file">
</c>
<c Name = "write">
</c>
<d>
</d>
</A>
_______________________________________________
XML-SIG maillist  -  XML-SIG@...
http://mail.python.org/mailman/listinfo/xml-sig

Re: need help with removeChild()

by Mike Brown-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

narendar rao wrote:
> 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.

With any XML tree API, be it DOM or lxml or ElementTree or Amara or whatever,
you are working with an in-memory model of the document. This model was
derived from the original serialized XML (the file) at parse time. Changes to
the model are confined to the model; they're in memory only. When you're done
making changes, you should produce a new serialization; the means of doing
this varies depending on the API you're using, but you can surely search
through the API docs or tutorials for how to serialize the tree. You can put
that serialization into a file, overwriting the old file if you want.

> Can some one help me in writing this code?

I won't, but would maybe someone else is feeling more charitable.
_______________________________________________
XML-SIG maillist  -  XML-SIG@...
http://mail.python.org/mailman/listinfo/xml-sig

Re: need help with removeChild()

by narendar rao-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks Mike.

I was able to do it. I needed little more understanding on how to do it.

thanks,
Narender
_______________________________________________
XML-SIG maillist  -  XML-SIG@...
http://mail.python.org/mailman/listinfo/xml-sig