Quantcast

Parsing and updating XML files with python

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Parsing and updating XML files with python

Balachandran Sivakumar-2
Hi,

             I have to parse an xml file, get to a spefic node, and
set it to a different value and write it back to the file. What is the
best way to do it ? The xml file can be a few hundred lines(with
proper formatting).

For parsing, I use lxml with xpath. I am doing something like this.

from lxml import etree

<---Open the XML file etc.--->
p_data = etree.parse(xml_data)

I use xpath like
p.xpath(/foo/bar[@id=xyz])

But I don't really know how to update an entry and write it back to
the xml file ? Can someone suggest some ways please ? Also, it is
necessary that it should be XPath based. Thanks

--
Thank you
Balachandran Sivakumar

Arise Awake and stop not till the goal is reached.
                                                             - Swami Vivekananda

Mail: [hidden email]
Blog: http://benignbala.wordpress.com/
_______________________________________________
BangPypers mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/bangpypers
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Parsing and updating XML files with python

Gora Mohanty-5
On Thu, Nov 17, 2011 at 4:47 PM, Balachandran Sivakumar
<[hidden email]> wrote:
> Hi,
>
>             I have to parse an xml file, get to a spefic node, and
> set it to a different value and write it back to the file. What is the
> best way to do it ? The xml file can be a few hundred lines(with
> proper formatting).
[...]
> But I don't really know how to update an entry and write it back to
> the xml file ? Can someone suggest some ways please ? Also, it is
> necessary that it should be XPath based. Thanks
[...]

To update an element, just assign a value to it, e.g.,
  myelem.text = "New value"
You can then save the modified XML to a new file.
Please see http://infohost.nmt.edu/tcc/help/pubs/pylxml/web/modifying.html

That same tutorial,
http://infohost.nmt.edu/tcc/help/pubs/pylxml/web/index.html ,
 also describes various ways of addressing elements. This is also a useful
tutorial: http://lxml.de/tutorial.html

Regards,
Gora
_______________________________________________
BangPypers mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/bangpypers
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Parsing and updating XML files with python

Balachandran Sivakumar-2
Hi,

On Thu, Nov 17, 2011 at 7:27 PM, Gora Mohanty <[hidden email]> wrote:
> On Thu, Nov 17, 2011 at 4:47 PM, Balachandran Sivakumar
> <[hidden email]> wrote:
>
> To update an element, just assign a value to it, e.g.,
>  myelem.text = "New value"
> You can then save the modified XML to a new file.
> Please see http://infohost.nmt.edu/tcc/help/pubs/pylxml/web/modifying.html
>
            Thanks for the info and the link.

> That same tutorial,
> http://infohost.nmt.edu/tcc/help/pubs/pylxml/web/index.html ,
>  also describes various ways of addressing elements. This is also a useful
> tutorial: http://lxml.de/tutorial.html
>

         I used the lxml.de tutorial in that link. But was not aware
the it gets updated in place. So, was expecting some method to do that
:( Thanks


--
Thank you
Balachandran Sivakumar

Arise Awake and stop not till the goal is reached.
                                                             - Swami Vivekananda

Mail: [hidden email]
Blog: http://benignbala.wordpress.com/
_______________________________________________
BangPypers mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/bangpypers
Loading...