Parse XML using Python
William Park
opengeometry at yahoo.ca
Thu Dec 9 15:24:10 EST 2004
anilby at gmail.com wrote:
> Hi,
>
> I wanted to write a script that will read the below file:
>
> <abcd label="ABC">
> .
> <efg label="EFGA">
> ....
> <decg label="ABDG">
> ..
> </decg>
> ..
>
> </efg>
>
> ..
> <mon1 label="MON">
> ..
> </mon1>
> ..
> </abcd>
> .
> .
> <xyz label="A1">
> .
> <eg1 label="FGA">
> ....
> <dg label="BG">
> ..
>
> </dg>
>
> ..
>
> </eg1>
>
> </xyz>
>
> ..
> and so on
>
> The output of the script shud be
>
> ABC
> ..EFGA
> ....ABDG
> ..MON
>
> A1
> ..FGA
> ....BG
>
> Please help me in writing a Python script for the above task.
Take a look at
http://home.eol.ca/~parkw/park-january.html
on "Expat XML" section towards the end. Translating it to Python is
left for homework.
In essence,
indent=..
start () {
local "${@:2}"
echo "${indent|*XML_ELEMENT_DEPTH-1}$label"
}
xml -s start "`< file.xml`"
which prints
..ABC
....EFGA
......ABDG
....MON
..A1
....FGA
......BG
with modified input, ie. wrapping XML pieces into single root tree.
--
William Park <opengeometry at yahoo.ca>
Open Geometry Consulting, Toronto, Canada
Linux solution for data processing.
More information about the Python-list
mailing list