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

javascript - Confirm a form & display message if form is valid with JQuery -

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

ionic framework - Meteor - Error: Failed to execute 'insertBefore' on 'Node' -