Quantcast

Modify node attributes in XML file while parsing

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Modify node attributes in XML file while parsing

Abhishek Kane
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

_______________________________________________
XML-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/xml-sig
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Modify node attributes in XML file while parsing

Stefan Behnel-3

Abhishek Kane wrote:
> 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?

Could you explain a bit more about your intentions? What is the code doing?
I understand that the XML data is in a file that you parse, but where is
the data going afterwards? Do you use it in your program? Do you save it
back into a file?

Stefan
_______________________________________________
XML-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/xml-sig
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Modify node attributes in XML file while parsing

Josh English
In reply to this post by Abhishek Kane
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 <[hidden email]> 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 <[hidden email]>
> 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 <[hidden email]> 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
>> [hidden email]
>> http://joshenglish.livejournal.com
>
>
>
> --
> No defeat is final; until u stop fighting.
> - AGK
>
>



--
Josh English
[hidden email]
http://joshenglish.livejournal.com
_______________________________________________
XML-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/xml-sig
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Modify node attributes in XML file while parsing

Abhishek Kane
In reply to this post by Abhishek Kane
<?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 <[hidden email]> 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 <[hidden email]> 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
[hidden email]
http://joshenglish.livejournal.com



--
No defeat is final; until u stop fighting.
- AGK


_______________________________________________
XML-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/xml-sig
Loading...