I have an XML file with the following structure:
<root>
<subroot id="someID">
<val1 value="a"/>
<val2 value="b"/>
<val3 value="c"/>
<val4 value="1"/>
<val5 value="2"/>
<val6 value="3"/>
<otherval value="xyz"/>
</subroot>
<subroot id="anotherID">
<val1 value="aa"/>
<val2 value="bb"/>
<val3 value="cc"/>
<val4 value="11"/>
<val5 value="22"/>
<val6 value="33"/>
<otherval value="xxyyzz"/>
</subroot>
.
.
.
.
</root>
I am trying to retrieve the values in each tag. For example, my desired output would be:
val1=a
val2=b
val3=c
val4=1
val5=2
val6=3
otherval=xyz
Here is my non-working code that produces blanks, ie val1="", val2=""...:
def getValues(self):
from xml.dom.minidom import parseString
import json
file = open('myfile.xml','r')
data = file.read()
dom = parseString(data)
rows = dom.getElementsByTagName("root")[0].getElementsByTagName("subroot")
valueString = ""
for row in rows:
valueString = valueString+json.dumps(
{
'val1': row.getAttribute("val1"),
'val2': row.getAttribute("val2"),
'val3': row.getAttribute("val3"),
'val4': row.getAttribute("val4"),
'val5': row.getAttribute("val5"),
'val6': row.getAttribute("val6"),
'other': row.getAttribute("otherval")},
sort_keys=True,
indent=4)+","
response_generator = ( "["+valueString[:-1]+"]" )
return HttpResponse(response_generator)
otherval=xyz
I know that thisactually produces JSON but thats not important. The important thing is being able to extract the values and then I can do whatever with them afterwards.
Can someone show me what I’m missing here?
Also, should I change my XML so all the val1,val2,val3.... are just called val?
Thanks.
My suggestion is to structure your data as follows:
Then, to parse, I suggest the etree library – it’s in the standard library for Python and I’ve found it far nicer to work with than anything else. It’s just a simple case of iterating through the subroots and value elements and extracting the data.
all will then be:
You could also do this as a dict comprehension:
Note that this is a little hard to read and will fall apart if you try to do anything more complex.