0

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.

1 Answer 1

1

You can use ast.literal_eval

from ast import literal_eval

Then

self.i1 = literal_eval(specific.firstChild.nodeValue)
Sign up to request clarification or add additional context in comments.

1 Comment

Great! Works perfectly. Would you probably also know how to import only the 2nd one (as mentioned in "last but not least")?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.