sam
sam

Reputation: 655

How to get element value using minidom in python and store them into a list?

Here is my code snippet:-

from xml.dom import minidom
from xml.dom.minidom import parse
xmldoc = minidom.parse('C:\\Users\\folder\\Documents\\Python Training\\XMLFiles\\sample.xml')

dimension = xmldoc.getElementsByTagName("tns:RefDimensionSet")[0]
fields = dimension.getElementsByTagName("tns:Field")

for field in fields:
    print field

This produces this output which seems to be alright:-

<DOM Element: tns:Field at 0x2a46988>
<DOM Element: tns:Field at 0x2a46b08>
<DOM Element: tns:Field at 0x2a46c88>
<DOM Element: tns:Field at 0x2a46e08>
<DOM Element: tns:Field at 0x2a46f88>
<DOM Element: tns:Field at 0x2a47148>
<DOM Element: tns:Field at 0x2a472c8>
<DOM Element: tns:Field at 0x2a47448>
<DOM Element: tns:Field at 0x2a475c8>
<DOM Element: tns:Field at 0x2a47748>

It displays the address of the elements, I want to display the values themselves instead and then store those values in a list as string elements.

Any help?

Upvotes: 1

Views: 745

Answers (1)

juankysmith
juankysmith

Reputation: 12458

Function getElementsByTagName returns an object of type DOM Element, this is why you get this result. I.e. if you want to access to the value of an attribute call id of that element you can try this:

field.attributes['id'].value

Upvotes: 2

Related Questions