Reputation: 171
I'm parsing an XML file using DOM in Python. Some of my XML nodes contain a list.
An example looks like this:
<items>
<item>
<name>name001</name>
<rows>10</rows>
<columns>14</columns>
<information>
<i1>[[3, 2]]</i1>
<i2>[[6, 13]]</i2>
<i3>[[3, 5]]</i3>
<i4>[[6, 10], [8, 6]]</i4>
<i5>[[6, 12]]</i5>
</information>
</item>
<item>
<name>name072</name>
<rows>10</rows>
<columns>13</columns>
<information>
<i1>[[3, 5]]</i1>
<i2>[[8, 2]]</i2>
<i3>[[6, 6], [8, 11]]</i3>
<i4>[[4, 7]]</i4>
<i5>[]</i5>
</information>
</item>
</items>
Right now I'm parsing the XML file like this:
dom = parse('file.xml')
el = dom.documentElement
allInformation = el.getElementsByTagName('information')
for information in allInformation:
for specific in information.childNodes:
if specific.nodeType == specific.ELEMENT_NODE:
if specific.nodeName == "i1":
self.i1 = specific.firstChild.nodeValue
Note: i1 is a variable in my file: self.i1 = [] The output right now for self.i1 is: [[3, 5]]
Now, I'm trying to select i1[0][0], which gives '[' as a result. My i1 is interpreted as a String but I'm trying to interpret it as a list. How could I fix this?
Last but not least, I'm also still searching a way to parse for example only "name072". Now I always parse my entire list and I'm only using the last one.
Edit: I tried to make this clearer. If you still have any suggestions/questions, please tell me.
Upvotes: 0
Views: 48
Reputation: 117856
You can use ast.literal_eval
from ast import literal_eval
Then
self.i1 = literal_eval(specific.firstChild.nodeValue)
Upvotes: 1