Re: [XML-SIG] Modify node attributes in XML file while parsing
by Josh English other posts by this author
Sep 16 2009 5:25PM messages near this date
Re: [XML-SIG] Modify node attributes in XML file while parsing
|
Your confirmation is required to join the XML-SIG mailing list
& XSLT Everything works, but you need to write this over data.xml. I don't
remember how to do this in minidom. I think writing to an output file
is the only way to do this:
o = open('data.xml')
o.write(xmldoc.toxml())
o.close()
On Wed, Sep 16, 2009 at 9:58 AM, Abhishek Kane <abhishek.kane@[...].com> wrote:
> <?xml version='1.0'?>
> <main_tag>
>
> <service name='abc' version='1'>
> Â Â Â <instance name='default' value='false'/>
> </service>
> <service name='xyz' version='2'>
> Â Â Â <instance name='default' value='false'/>
> </service>
>
> </main_tag>
>
>
> Â is how the XML file looks like. I want to modify the value of instance
> based on the service name.
>
> So if i search for abc service & modify instance value like follows :
>
>
> import sys, logging, traceback
> from xml.dom import minidom
>
> data_file = "/tmp/data.xml"
>
> def main():
> Â Â Â try:
> Â Â Â Â Â Â xmldoc = minidom.parse(data_file)
> Â Â Â except IOError:
> Â Â Â Â Â Â print "err"
> Â Â Â Â Â Â sys.exit(1)
>
> Â Â Â start = xmldoc.firstChild
>
> Â Â Â for element in xmldoc.getElementsByTagName("service"):
> Â Â Â Â Â Â service = element.getAttribute("name")
> Â Â Â Â Â Â print service
> Â Â Â Â Â Â if service == "abc":
> Â Â Â Â Â Â Â Â Â for instance in element.childNodes:
> Â Â Â Â Â Â Â Â Â Â Â Â if instance.nodeType == 1:
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â print instance.getAttribute("value")
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â instance.setAttribute("value", "true")
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â print instance.getAttribute("value")
>
> if __name__ == "__main__":
> Â Â Â logging.basicConfig(filename=log_file,level=logging.DEBUG)
> Â Â Â main()
>
> The first print gives false & later one gives true. But the data.xml remains
> unchanged. How can I modify the node in data.xml?
>
> Thanks,
> Abhishek
>
> On Wed, Sep 16, 2009 at 9:47 PM, Josh English <joshua.r.english@[...].com>
> wrote:
> >
> > If you are just searching for XML nodes, you are searching. If you are
> > interpreting them into something new, you are parsing.
> >
> > If you want to change an attribute, parse the XML file and make changes
> > there.
> >
> > Either that, or read the node attribute that you are given, make the
> > changes you need, but don't bother with writing a new XML file.
> >
> > Perhaps a sample of what you are trying to do would help up see the
> > solution.
> >
> > On 9/16/09, Abhishek Kane <abhishek.kane@[...].com> wrote:
> > > Hi Guys,
> > >
> > > I got a bit idea about parsing XML file from online search. I need to
> > > modify
> > > node attributes in an XML file while I read it. I could read node
> > > attributes; but when I modify them using setAttribute, the local copy of
> > > that node gets changed.
> > > Is there any way to directly modify the node attributes in XML file or
> > > shall
> > > I just create another XML file where I will create nodes as I go through
> > > the
> > > nodes in old XML file?
> > >
> > > Thanks,
> > > Abhishek
> > >
> >
> >
> > --
> > Josh English
> > Joshua.R.English@[...].com
> > http://joshenglish.livejournal.com
>
>
>
> --
> No defeat is final; until u stop fighting.
> - AGK
>
>
--
Josh English
Joshua.R.English@[...].com
http://joshenglish.livejournal.com
_______________________________________________
XML-SIG maillist - XML-SIG@[...].org
http://mail.python.org/mailman/listinfo/xml-sig
Thread:
Abhishek Kane
Stefan Behnel
Abhishek Kane
Josh English
|