[XML-SIG] Learning to use elementtree
J. Cliff Dyer
jcd at unc.edu
Wed Apr 2 20:46:57 CEST 2008
On Wed, 2008-04-02 at 14:39 -0400, Doran, Harold wrote:
> Cliff
>
> This was very helpful, thank you. I have modified the code accordingly
> and all is working as expected. I want to make one modification, but
> seem to be having some problems with generalization of the code.
>
> The current program operates as follows:
>
> xmlReader.py
> from xml.etree.ElementTree import ElementTree as ET
>
> filename = raw_input("Please enter the AM XML file: ")
> new_file = raw_input("Save this file as: ")
>
> # create a new file defined by the user
> f = open(new_file, 'w')
>
> et = ET(file=filename)
>
> for statentityref in \
> et.findall('admin/responseanalyses/analysis/analysisdata/statentityref')
> :
> for statval in statentityref.findall('statval'):
> print >> f, statentityref.attrib['id'], '\t',
> statval.attrib['type'], '\t', statval.attrib['value']
>
> f.close()
>
> This is based on your recommendation and works smoothly. Now, in the xml
> file (which I have again attached), there are other statistics nested
> inside admin/responseanalyses/analysis/analysisdata/statentityref that I
> want in addition to what is already being extracted.
>
> For example, (see snippet of xml below) the current program above pulls
> out the attributes for id = 13963, skips the information below it where
> id = 0 or id =1 and then pulls out the information for id = 13962. My
> goal is to extract the information where id = 0 or 1 in addition to the
> attribute for id=13963.
>
>
> - <statentityref id="1.000000" type="itemscorept">
> <statval type="UncollapsedMeanScore" value="34.941426" se="0.256340"
> />
> <statval type="ScorePtPct" value="0.981667" se="0.003874" />
> <statval type="ScorePtBiserial" value="0.496309" />
> <statval type="ScorePtAdjBiserial" value="0.452588" />
> </statentityref>
I don't have your code to work from, but based on what you've said, I
think you might be having troubles with the difference between strings
and integers. 0 == 0.000000, but "0" != "0.000000". XML is made up of
strings, so unless you're converting to ints or floats before you try to
match, you're going to miss on that account.
Another common source of errors (at least for me :)) is not being aware
of your current context node. Figure out where you are, and what's
available from that point.
Cheers,
Cliff
More information about the XML-SIG
mailing list