1
요소에 여러 텍스트 노드가있는 xml이 있습니다. python2 etree
을 사용하여 같은 순서로 트리를 탐색하고 싶습니다.python2에서 xml 노드 (요소와 텍스트 노드 모두)를 반복하는 방법 etree
그래서,이 입력 :
<body>
hello
<b>world</b>
bye
</body>
내가 이 정확한 순서이 출력을 생성 할 수 있어야한다 : 그러나
tag: body
text: hello
tag: b
text: world
text: bye
, 나는 etree
기능에 표시되지 않습니다 요소와 텍스트 노드를 모두 반복합니다.
어떻게하면됩니까? 나는 같은 뭔가를 찾고 있어요 (iterateElementsAndTextNodes
가하는 기능은하지 존재) :
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, "")
가능한 중복 http://stackoverflow.com/questions/4695826/ 효율적인 방법 - iterate-throught-xml 요소) – stovfl