Matt Robertson Matt Robertson - 1 year ago 148
Python Question

Python XML Pull Parser

I am trying to parse an XML file using Python. Due to the size of the XML, I want to use a Pull Parser. I found this one.

My code starts with

doc = pulldom.parse("myfile.xml")
for event, node in doc:
# code here...

I am using

if (node.localName == "b"):

to get the XML tag name, and it works fine.

What I can't find how to do is get the text from between the tags. Using

I can use
to get the full XML for the node, but I only want the text between the tags. Is there a way to do this other than using a regex replace to take the tags out of

Answer Source

You have two nodes with local name "b" for every tag with text - a START_ELEMENT and an END_ELEMENT. Normally you should receive something like this:


So you are looking for the characters after a matching start-element. You may want to try something like this:

from xml.dom.pulldom import CHARACTERS, START_ELEMENT, parse

doc = parse("myfile.xml")
text_expected = False
for event, node in doc:
    print event, node
    if text_expected:
        text_expected = False
        if event != CHARACTERS:
            # strange .. there should be some
        text_expected = (event == START_ELEMENT) and (node.localName == "b")

With this myfile.xml


I get the output


Note that you might need to strip() each string and you must ignore every other CHARACTERS-event. Every linebreak and whitespace between two elements generate a CHARACTERS-event.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download