I have a xml, in where an element contains multiple text nodes. Using python2 etree
, I want to navigate the tree with the same order.
So, for this input:
<body>
hello
<b>world</b>
bye
</body>
I need to be able to produce this output in this exact order:
tag: body
text: hello
tag: b
text: world
text: bye
However, I do not see in etree
a function to iterate on both elements and text nodes.
How can I do that?
I am looking for something such as (the function iterateElementsAndTextNodes
does not exists):
from lxml import etree
import utils
doc = etree.XML("""<body>hello<b>world</b>bye</body>""")
def printNode(node, prefix):
if isinstance(node, str):
print prefix + "text: " + node
else:
print prefix + "tag:" + node.tag
for c in node.iterateElementsAndTextNodes():
printNode(c, prefix + " ")
printNode(doc, "")