[XML-SIG] Learning to use elementtree
Doran, Harold
HDoran at air.org
Wed Apr 2 21:28:36 CEST 2008
Indeed, navigating the xml is tough (for me). I have been able to get
the following to work. I put in "Sub Element" to indicate the new
section of data. But, from looking at the text output, one doesn't know
which item these sub elements belong to. I think the solution is to
create an index like 13965-0 to show that this is the subinformation
from the item above it. That seems to be where I am getting stuck.
Although, I am open to other suggestions on how to best represent the
output.
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.write("\n\n")
f.write("Sub Element\n\n")
for statentityref in \
et.findall('admin/responseanalyses/analysis/analysisdata/statentityref/s
tatentityref'):
for statval in statentityref.findall('statval'):
print >> f, statentityref.attrib['id'], '\t',
statval.attrib['type'], '\t', statval.attrib['value']
f.close()
> -----Original Message-----
> From: J. Cliff Dyer [mailto:jcd at unc.edu]
> Sent: Wednesday, April 02, 2008 2:47 PM
> To: Doran, Harold
> Cc: xml-sig at python.org
> Subject: Re: [XML-SIG] Learning to use elementtree
>
> 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