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>&lt;?xml version=3D&#39;1.0&#39;?&gt;<br>&lt;main_tag&gt;<br><br>&lt;ser=
vice name=3D&#39;abc&#39; version=3D&#39;1&#39;&gt;<br>=A0=A0=A0 &lt;instan=
ce name=3D&#39;default&#39; value=3D&#39;false&#39;/&gt;<br>&lt;/service&gt=
;<br>&lt;service name=3D&#39;xyz&#39; version=3D&#39;2&#39;&gt;<br>

=A0=A0=A0 &lt;instance name=3D&#39;default&#39; value=3D&#39;false&#39;/&gt=
;<br>&lt;/service&gt;<br><br>&lt;/main_tag&gt;<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 &amp; 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 &quot;/tmp/data.xml&quot;<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 &quot;err&quot;<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(&quot;servic=
e&quot;):<br>

=A0=A0=A0 =A0=A0=A0 service =3D element.getAttribute(&quot;name&quot;)<br>=
=A0=A0=A0 =A0=A0=A0 print service<br>=A0=A0=A0 =A0=A0=A0 if service =3D=3D =
&quot;abc&quot;:<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(&quot;value&quot;)<br>=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =
instance.setAttribute(&quot;value&quot;, &quot;true&quot;)<br>=A0=A0=A0 =A0=
=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 print instance.getAttribute(&quot;valu=
e&quot;)<br>

<br>if __name__ =3D=3D &quot;__main__&quot;:<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> &amp; 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">&lt;<a href=3D"mailto:joshua=
[email protected]">[email protected]</a>&gt;</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&#39;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 &lt;<a href=3D"mailto:[email protected]">ab=
[email protected]</a>&gt; wrote:<br>
&gt; Hi Guys,<br>
&gt;<br>
&gt; I got a bit idea about parsing XML file from online search. I need to =
modify<br>
&gt; node attributes in an XML file while I read it. I could read node<br>
&gt; attributes; but when I modify them using setAttribute, the local copy =
of<br>
&gt; that node gets changed.<br>
&gt; Is there any way to directly modify the node attributes in XML file or=
 shall<br>
&gt; I just create another XML file where I will create nodes as I go throu=
gh the<br>
&gt; nodes in old XML file?<br>
&gt;<br>
&gt; Thanks,<br>
&gt; Abhishek<br>
&gt;<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==--