Re: Modify node attributes in XML file while parsing
Abhishek Kane <[email protected]> Thu, 17 Sep 2009 13:17:40 +0530
| Newsgroups | gmane.comp.python.xml |
|---|---|
| Message-ID | <[email protected]> |
--===============1479797949==
Content-Type: multipart/alternative; boundary=00504502cbefdfc6470473c13ad9
--00504502cbefdfc6470473c13ad9
Content-Type: text/plain; charset=ISO-8859-1
*<?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 <[email protected]>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 <[email protected]> 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
> [email protected]
> http://joshenglish.livejournal.com
>
--
No defeat is final; until u stop fighting.
- AGK
--00504502cbefdfc6470473c13ad9
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
<b><?xml version=3D'1.0'?><br><main_tag><br><br><ser=
vice name=3D'abc' version=3D'1'><br>=A0=A0=A0 <instan=
ce name=3D'default' value=3D'false'/><br></service>=
;<br><service name=3D'xyz' version=3D'2'><br>
=A0=A0=A0 <instance name=3D'default' value=3D'false'/>=
;<br></service><br><br></main_tag><br></b><br><br>=A0is how the=
XML file looks like. I want to modify the <b>value </b>of <b>instance </b>=
based on the <b>service name</b>.<br>
<br>So if i search for <b>abc</b> service & modify instance value like =
follows :<br><br><br><b>import sys, logging, traceback<br>from xml.dom impo=
rt minidom<br><br>data_file =3D "/tmp/data.xml"<br><br>def main()=
:<br>
=A0=A0=A0 try:<br>=A0=A0=A0 =A0=A0=A0 xmldoc =3D minidom.parse(data_file)<b=
r>=A0=A0=A0 except IOError:<br>=A0=A0=A0 =A0=A0=A0 print "err"<br=
>=A0=A0=A0 =A0=A0=A0 sys.exit(1)<br><br>=A0=A0=A0 start =3D xmldoc.firstChi=
ld<br><br>=A0=A0=A0 for element in xmldoc.getElementsByTagName("servic=
e"):<br>
=A0=A0=A0 =A0=A0=A0 service =3D element.getAttribute("name")<br>=
=A0=A0=A0 =A0=A0=A0 print service<br>=A0=A0=A0 =A0=A0=A0 if service =3D=3D =
"abc":<br>=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 for instance in element.c=
hildNodes:<br>=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 if instance.nodeType =
=3D=3D 1:<br>
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 print instance.getAttribu=
te("value")<br>=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =
instance.setAttribute("value", "true")<br>=A0=A0=A0 =A0=
=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 print instance.getAttribute("valu=
e")<br>
<br>if __name__ =3D=3D "__main__":<br>=A0=A0=A0 logging.basicConf=
ig(filename=3Dlog_file,level=3Dlogging.DEBUG)<br>=A0=A0=A0 main()<br></b><b=
r>The first print gives <b>false</b> & later one gives <b>true</b>. But=
the <b>data.xml</b> remains unchanged. How can I modify the node in data.x=
ml?<br>
<br>Thanks,<br>Abhishek<br><br><div class=3D"gmail_quote">On Wed, Sep 16, 2=
009 at 9:47 PM, Josh English <span dir=3D"ltr"><<a href=3D"mailto:joshua=
[email protected]">[email protected]</a>></span> wrote:<br><=
blockquote class=3D"gmail_quote" style=3D"border-left: 1px solid rgb(204, 2=
04, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
If you are just searching for XML nodes, you are searching. If you are<br>
interpreting them into something new, you are parsing.<br>
<br>
If you want to change an attribute, parse the XML file and make changes the=
re.<br>
<br>
Either that, or read the node attribute that you are given, make the<br>
changes you need, but don't bother with writing a new XML file.<br>
<br>
Perhaps a sample of what you are trying to do would help up see the solutio=
n.<br>
<div><div></div><div class=3D"h5"><br>
On 9/16/09, Abhishek Kane <<a href=3D"mailto:[email protected]">ab=
[email protected]</a>> wrote:<br>
> Hi Guys,<br>
><br>
> I got a bit idea about parsing XML file from online search. I need to =
modify<br>
> node attributes in an XML file while I read it. I could read node<br>
> attributes; but when I modify them using setAttribute, the local copy =
of<br>
> that node gets changed.<br>
> Is there any way to directly modify the node attributes in XML file or=
shall<br>
> I just create another XML file where I will create nodes as I go throu=
gh the<br>
> nodes in old XML file?<br>
><br>
> Thanks,<br>
> Abhishek<br>
><br>
<br>
<br>
</div></div><font color=3D"#888888">--<br>
Josh English<br>
<a href=3D"mailto:[email protected]">[email protected]</a=
><br>
<a href=3D"http://joshenglish.livejournal.com" target=3D"_blank">http://jos=
henglish.livejournal.com</a><br>
</font></blockquote></div><br><br clear=3D"all"><br>-- <br>No defeat is fin=
al; until u stop fighting.<br>- AGK<br><br>
--00504502cbefdfc6470473c13ad9--
--===============1479797949==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
XML-SIG maillist - [email protected]
http://mail.python.org/mailman/listinfo/xml-sig
--===============1479797949==--