Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting text between xml tags with minidom [duplicate]

Tags:

python

xml

I have this sample xml document snippet

<root>
    <foo>bar</foo>
    <foo>baz</foo>
</root>

I'm using python's minidom method from xml.dom. I am reading in tags with getElementsByTagName("foo"). How do I get the text between the tags? And if the tags were nested, how would I get those?

like image 587
coffee Avatar asked Nov 04 '22 18:11

coffee


2 Answers

So if you need to get the text out then you can do the following:

import xml.dom.minidom
document = "<root><foo>bar</foo><foo>baby</foo></root>"
dom = xml.dom.minidom.parseString(document)

def getText(nodelist):
    rc = []
    for node in nodelist:
        if node.nodeType == node.TEXT_NODE:
            rc.append(node.data)
    return ''.join(rc)

def handleTok(tokenlist):
    texts = ""
    for token in tokenlist:
        texts += " "+ getText(token.childNodes)
    return texts
foo = dom.getElementsByTagName("foo")
text = handleTok(foo)
print text

They have a good example on the site: http://docs.python.org/library/xml.dom.minidom.html

EDIT: For nested tags, check the example on the site.

like image 164
kmdent Avatar answered Nov 15 '22 06:11

kmdent


Here is how with ElementTree:

xml='''\
<root>
    <foo>bar</foo>
    <foo>baz</foo>
</root>'''

import xml.etree.ElementTree as ET

for child in ET.fromstring(xml):
    print child.tag, child.text

Prints:

foo bar
foo baz
like image 24
dawg Avatar answered Nov 15 '22 06:11

dawg