[Tutor] Help with Elementtree ...how to access the attributes..
doug shawhan
doug.shawhan at gmail.com
Fri Nov 10 19:00:32 CET 2006
I'm having some difficulties with elementtree as well.
I'm attempting to parse a fairly sizeable xml file (it's the ebay "category
tree" report. I've cached a copy at
http://www.crackrabbit.com/misc/CatTree.xml). 900K or so! :-)
For some reason I'm unable to read any elements from the file. I have no
problems with Asraramed's example. (I'm not attempting to extract values
here, just to make sure that there is actual parsing going on!)
#!/usr/bin/env python2.4
from elementtree import ElementTree as et
tree = et.parse("kadri.xml")
companies = tree.findall('company')
for c in companies:
print c
which gives me:
<Element company at -74207a74>
<Element company at -74207774>
<Element company at -74207614>
Cool beans. There's something in there!
Now when I attempt the same with the data found at the above link:
#!/usr/bin/env python2.4
from elementtree import ElementTree as et
tree = et.parse('CatTree.xml')
categories = tree.findall("CategoryArray")
for cat in categories:
print cat
I receive nothing.
I've made a smaller version that I could easily get me brain around:
<?xml version="1.0" encoding="UTF-8"?>
<GetCategoriesResponse xmlns="urn:ebay:apis:eBLBaseComponents">
<Timestamp>2006-11-07T07:45:40.908Z</Timestamp>
<Ack>Success</Ack>
<Version>485</Version>
<Build>e485_core_Bundled_3782281_R1</Build>
<CategoryArray>
<Category>
<BestOfferEnabled>true</BestOfferEnabled>
<CategoryID>6000</CategoryID>
<CategoryLevel>1</CategoryLevel>
<CategoryName>eBay Motors</CategoryName>
<CategoryParentID>6000</CategoryParentID>
<Expired>false</Expired>
<IntlAutosFixedCat>false</IntlAutosFixedCat>
<LeafCategory>false</LeafCategory>
<Virtual>false</Virtual>
<ORPA>false</ORPA>
<LSD>true</LSD>
</Category>
</CategoryArray>
</GetCategoriesResponse>
which also seems to parse cleanly and appears to be similar to what
Asraramed had constructed, but still does not yeild any results no matter
which element I attempt to find with findall().
What am I doing wrong? Thanks!
On 11/9/06, John Fouhy <john at fouhy.net> wrote:
>
> On 10/11/06, Asrarahmed Kadri <ajkadri at googlemail.com> wrote:
> > I am trying to parse XML documents using elementtree.
> > I have just started with it. Can somebody help me with how to select
> nodes
> > with a particular atribute set to some value. For example, I have the
> > following file. Now I want to access the founder element of the company
> > whose attribute is set to 'ndtv'. Can somebody help me with this?
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061110/384a5311/attachment.html
More information about the Tutor
mailing list