[Tutor] xml parsing from xml
Neil D. Cerutti
neilc at norwich.edu
Wed May 7 16:37:32 CEST 2014
On 5/7/2014 9:13 AM, jitendra gupta wrote:
> Hi
>
> I just want to create a new xm file from existing xml file. so basically
> i want to put contry details in countryName.xml from these file.
>
> I thought to do via read a line by line with normal file handling. but
> there a problem with that. So i want to handle python XML . Could you
> please suggest on this.
>
> Any Idea is welcome
>
> Thanks & regards
> Jitendra
>
> <?xml version="1.0"?>
> <data>
> <country name="Liechtenstein">
> <rank updated="yes">2</rank>
> <year>2008</year>
> <gdppc>141100</gdppc>
> <neighbor name="Austria" direction="E"/>
> <neighbor name="Switzerland" direction="W"/>
> </country>
> <country name="Singapore">
> <rank updated="yes">5</rank>
> <year>2011</year>
> <gdppc>59900</gdppc>
> <neighbor name="Malaysia" direction="N"/>
> </country>
> <country name="Panama">
> <rank updated="yes">69</rank>
> <year>2011</year>
> <gdppc>13600</gdppc>
> <neighbor name="Costa Rica" direction="W"/>
> <neighbor name="Colombia" direction="E"/>
> </country>
> </data>
We need more details to be able to help.
What should be in the resulting XML file or files? Do you want to create
one XML file for each country in your source file?
I agree with you that trying to parse XML manually, line-by-line, is a
bad idea. Python comes with xml.etree.ElementTree, a library useful for
parsing and generating xml documents. Take a look at the documentation
and examples in the Python docs to get started.
--
Neil Cerutti
More information about the Tutor
mailing list