Re: parsing XML with minidom
Rajanikanth Jammalamadaka <[email protected]> Wed, 3 Feb 2010 22:11:25 -0800
| Newsgroups | gmane.comp.python.xml |
|---|---|
| Message-ID | <[email protected]> |
--===============1365900188==
Content-Type: multipart/alternative; boundary=0016361e835673e5f8047ec034ae
--0016361e835673e5f8047ec034ae
Content-Type: text/plain; charset=ISO-8859-1
Try this:
from xml.etree.ElementTree import ElementTree
doc = ElementTree(file = "t.xml")
listOfTags = []
for item in doc.findall(".//route"):
listOfTags.append(item.get('tag'))
print listOfTags
where t.xml is your xml file.
Thanks,
Raj
On Wed, Feb 3, 2010 at 8:04 PM, kimmyaf <[email protected]> wrote:
>
> Hello, I am not real sure if my question belongs here or not, but this is
> best place I could find.
>
> I am a python beginner and trying to teach myself how to parse some XML
> with
> minidom.
>
> This is the code excerpt I am struggling with....
>
> ********************************************************
> dom = minidom.parseString(xml_response)
> handler.close()
>
> route_list = []
> tag = ['route']
>
> tmp_route=[]
> for route in dom.getElementsByTagName('body'):
> print 'in'
> tmp_route[route] =
> dom.getElementsByTagName(tag)[0].getAttribute('tag')
> route_list.append(tmp_route)
>
> *******************************************************************
> Here is the XML I am getting back when I call...
>
> '<?xml version="1.0" encoding="utf-8" ?> \r\n<body copyright="All data
> copyright MBTA 2010.">\r\n<route tag="39" title="39"/>\r\n<route tag="111"
> title="111"/>\r\n<route tag="114" title="114"/>\r\n<route tag="116"
> title="116"/>\r\n<route tag="117" title="117"/>\r\n</body>\r\n'
>
> See this formatted better by pasting this URL = >
>
>
> http://webservices.nextbus.com/service/publicXMLFeed?command=routeList&a=mbta
>
>
> I am taking the following error:
>
> File "C:/Users/Kim/Grad School/Python/bus python.py", line 54, in <module>
> get_available_routes()
> File "C:/Users/Kim/Grad School/Python/bus python.py", line 43, in
> get_available_routes
> tmp_route[route] = dom.getElementsByTagName(tag)[0].getAttribute('tag')
> IndexError: list index out of range
>
>
>
> I'm sure there is something obvious that I am doing wrong. All I want to do
> is grab all of the <route tag> values and put them into a list. Kind of
> new
> to parsing XML! I'm working off an example but the XML in the example code
> is a lot more in depth so can't really relate it to mine. I also would like
> any reference anyone has about how to parse with minidom!!
>
> Help! Thank you! %-|
> --
> View this message in context:
> http://old.nabble.com/parsing-XML-with-minidom-tp27447458p27447458.html
> Sent from the Python - xml-sig mailing list archive at Nabble.com.
>
> _______________________________________________
> XML-SIG maillist - [email protected]
> http://mail.python.org/mailman/listinfo/xml-sig
>
--
Rajanikanth
--0016361e835673e5f8047ec034ae
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Try this:<div><br></div><div><div>from xml.etree.ElementTree import Element=
Tree</div><div><br></div><div>doc =3D ElementTree(file =3D "t.xml"=
;)</div><div><br></div><div>listOfTags =3D []</div><div><br></div><div>for =
item in doc.findall(".//route"):</div>
<div>=A0=A0 =A0listOfTags.append(item.get('tag'))</div><div>=A0=A0 =
=A0</div><div>print listOfTags</div><div><br></div><div><br></div><div>wher=
e t.xml is your xml file.</div><div><br></div><div>Thanks,</div><div>Raj</d=
iv><div>
<br></div><br><div class=3D"gmail_quote">On Wed, Feb 3, 2010 at 8:04 PM, ki=
mmyaf <span dir=3D"ltr"><<a href=3D"mailto:[email protected]">flahe=
[email protected]</a>></span> wrote:<br><blockquote class=3D"gmail_quote=
" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
Hello, I am not real sure if my question belongs here or not, but this is<b=
r>
best place I could find.<br>
<br>
I am a python beginner and trying to teach myself how to parse some XML wit=
h<br>
minidom.<br>
<br>
This is the code excerpt I am struggling with....<br>
<br>
********************************************************<br>
=A0 dom =3D minidom.parseString(xml_response)<br>
=A0 =A0handler.close()<br>
<br>
=A0 =A0route_list =3D []<br>
=A0 =A0tag =3D ['route']<br>
<br>
=A0 =A0tmp_route=3D[]<br>
=A0 =A0for route in dom.getElementsByTagName('body'):<br>
=A0 =A0 =A0 =A0print 'in'<br>
=A0 =A0 =A0 =A0tmp_route[route] =3D<br>
dom.getElementsByTagName(tag)[0].getAttribute('tag')<br>
=A0 =A0 =A0 =A0route_list.append(tmp_route)<br>
<br>
*******************************************************************<br>
Here is the XML I am getting back when I call...<br>
<br>
'<?xml version=3D"1.0" encoding=3D"utf-8" ?> =
\r\n<body copyright=3D"All data<br>
copyright MBTA 2010.">\r\n<route tag=3D"39" title=3D&q=
uot;39"/>\r\n<route tag=3D"111"<br>
title=3D"111"/>\r\n<route tag=3D"114" title=3D&qu=
ot;114"/>\r\n<route tag=3D"116"<br>
title=3D"116"/>\r\n<route tag=3D"117" title=3D&qu=
ot;117"/>\r\n</body>\r\n'<br>
<br>
=A0 =A0See this formatted better by pasting this URL =3D ><br>
<br>
<a href=3D"http://webservices.nextbus.com/service/publicXMLFeed?command=3Dr=
outeList&a=3Dmbta" target=3D"_blank">http://webservices.nextbus.com/ser=
vice/publicXMLFeed?command=3DrouteList&a=3Dmbta</a><br>
<br>
<br>
I am taking the following error:<br>
<br>
=A0File "C:/Users/Kim/Grad School/Python/bus python.py", line 54=
, in <module><br>
=A0 =A0get_available_routes()<br>
=A0File "C:/Users/Kim/Grad School/Python/bus python.py", line 43=
, in<br>
get_available_routes<br>
=A0 =A0tmp_route[route] =3D dom.getElementsByTagName(tag)[0].getAttribute(=
'tag')<br>
IndexError: list index out of range<br>
<br>
<br>
<br>
I'm sure there is something obvious that I am doing wrong. All I want t=
o do<br>
is grab all of the <route tag> values and put them into a list. =A0Ki=
nd of new<br>
to parsing XML! I'm working off an example but the XML in the example c=
ode<br>
is a lot more in depth so can't really relate it to mine. I also would =
like<br>
any reference anyone has about how to parse with minidom!!<br>
<br>
Help! Thank you! %-|<br>
<font color=3D"#888888">--<br>
View this message in context: <a href=3D"http://old.nabble.com/parsing-XML-=
with-minidom-tp27447458p27447458.html" target=3D"_blank">http://old.nabble.=
com/parsing-XML-with-minidom-tp27447458p27447458.html</a><br>
Sent from the Python - xml-sig mailing list archive at Nabble.com.<br>
<br>
_______________________________________________<br>
XML-SIG maillist =A0- =A0<a href=3D"mailto:[email protected]">XML-SIG@pyth=
on.org</a><br>
<a href=3D"http://mail.python.org/mailman/listinfo/xml-sig" target=3D"_blan=
k">http://mail.python.org/mailman/listinfo/xml-sig</a><br>
</font></blockquote></div><br><br clear=3D"all"><br>-- <br>Rajanikanth<br>
</div>
--0016361e835673e5f8047ec034ae--
--===============1365900188==
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
--===============1365900188==--