python - how to iterate on xml nodes (both elements and text nodes) in python2 etree -


i have xml, in element contains multiple text nodes. using python2 etree, want navigate tree same order.

so, input:

<body>   hello   <b>world</b>   bye </body> 

i need able produce output in this exact order:

tag: body    text: hello    tag: b        text: world    text: bye 

however, not see in etree function iterate on both elements , text nodes.

how can that? looking such (the function iterateelementsandtextnodes 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         c in node.iterateelementsandtextnodes():             printnode(c, prefix + "   ")  printnode(doc, "") 

we can use child::node() in xpath select children of context node, whatever node type. read here. so, changing loop to:

for c in node.xpath("child::node()"):     printnode(c, prefix + "   ") 

code:

from lxml import etree import utils  doc = etree.xml("""<body>hello<b>world</b>bye</body>""") #print "doc is", etree.tostring(doc) def printnode(node, prefix):     if isinstance(node, etree._elementstringresult):         print prefix + "text: " + node     else:         print prefix + "tag: " + node.tag         c in node.xpath("child::node()"):             printnode(c, prefix + "   ") printnode(doc, "") 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -