Marshall
Marshall

Reputation: 137

Python MINIDOM Object How to get only the element name from DOM Object

I have a python DOM object output, I need to get only the "Elements" from it.

Example:

[<DOM Text node "u'\n\t\t\t'">, <DOM Element: StartTime at 0x397af30>, <DOM Text node "u'\n\t\t\t'">, <DOM Element: EndTime at 0x397afd0>, <DOM Text node "u'\n\t\t'">]

I need output like

StartTime
EndTime 

Can you please assist ? Thanks

Upvotes: 3

Views: 976

Answers (1)

har07
har07

Reputation: 89305

"I have a python DOM object output, I need to get only the "Elements" from it."

You can filter your list item where nodeType == xml.dom.minidom.Node.ELEMENT_NODE. For example, assuming that your 'DOM object output' stored in a variable named output, you can do as follow :

from xml.dom import minidom
.....
.....
result = [item for item in output if item.nodeType == minidom.Node.ELEMENT_NODE]

Upvotes: 3

Related Questions