I am trying to get an xml file that looks like this so that I can import it into a moodle gradebook:
<results>
<result>
<student>1</student>
<assignment>100</assignment>
<score>0</score>
</result>
<result>
<student>1</student>
<assignment>101</assignment>
<score>4</score>
</result>
<result>
<student>1</student>
<assignment>102</assignment>
<score>10</score>
</result>
<result>
<student>2</student>
<assignment>100</assignment>
<score>0</score>
</result>
<result>
<student>2</student>
<assignment>101</assignment>
<score>4</score>
</result>
<result>
<student>2</student>
<assignment>102</assignment>
<score>10</score>
</result>
</results>
I have tried to write some code in python, and after poring over the documentation, the best I can come up with is:
import xml.etree.cElementTree as ET
students = [1,2]
assignments=[100,101,102]
scores=[0,4,10]
results = ET.Element("results")
result = ET.SubElement(results,"result")
student = ET.SubElement(result,"student")
assignment = ET.SubElement(result,"assignment")
score = ET.SubElement(result,"score")
for s in students:
for a in range(len(assignments)):
student.text = str(s)
assignment.text = str(assignments[a])
score.text = str(scores[a])
results.append(result)
tree = ET.ElementTree(results)
tree.write('test.xml')
This almost gives me what I want, but the last entry seems to overwrite all the previous entries as shown:
<results>
<result>
<student>2</student>
<assignment>102</assignment>
<score>10</score>
</result>
<result>
<student>2</student>
<assignment>102</assignment>
<score>10</score>
</result>
<result>
<student>2</student>
<assignment>102</assignment>
<score>10</score>
</result>
<result>
<student>2</student>
<assignment>102</assignment>
<score>10</score>
</result>
<result>
<student>2</student>
<assignment>102</assignment>
<score>10</score>
</result>
<result>
<student>2</student>
<assignment>102</assignment>
<score>10</score>
</result>
<result>
<student>2</student>
<assignment>102</assignment>
<score>10</score>
</result>
</results>
I have tried to find a solution, but it seems that most of the questions address just writing an xml file with distinct elements. What I can't figure out is how to use the same element tag but to assign distinct values to each one.
You need to do two things:
results.append(result) from the for loop. The SubElement factory takes as its first argument a parent element
(as you know) and it appends the newly created instance to that
parent. Therefore you don't need to append it again (you won't get
any error, but you will get a different set of results than the ones
you specified).You loop should then look like:
for s in students:
for a in range(len(assignments)):
result = ET.SubElement(results,"result")
student = ET.SubElement(result,"student")
assignment = ET.SubElement(result,"assignment")
score = ET.SubElement(result,"score")
student.text = str(s)
assignment.text = str(assignments[a])
score.text = str(scores[a])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With