在使用ElementTree的Python2.6中,获取特定元素中的XML (作为字符串)的好方法是什么,就像您可以用innerHTML
在HTML和javascript中做的那样?
下面是我开始使用的XML节点的简化示例:
<label attr="foo" attr2="bar">This is some text <a href="foo.htm">and a link</a> in embedded HTML</label>
我想用这个字符串来结束:
This is some text <a href="foo.htm">and a link</a> in embedded HTML
我尝试迭代父节点并连接子节点的tostring()
,但这只给出了子节点:
# returns only subnodes (e.g. <a href="foo.htm">and a link</a>)
''.join([et.tostring(sub, encoding="utf-8") for sub in node])
我可以使用正则表达式来破解一个解决方案,但我希望有比这更简单的解决方案:
re.sub("</\w+?>\s*?$", "", re.sub("^\s*?<\w*?>", "", et.tostring(node, encoding="utf-8")))
发布于 2010-08-10 12:34:31
这样如何:
from xml.etree import ElementTree as ET
xml = '<root>start here<child1>some text<sub1/>here</child1>and<child2>here as well<sub2/><sub3/></child2>end here</root>'
root = ET.fromstring(xml)
def content(tag):
return tag.text + ''.join(ET.tostring(e) for e in tag)
print content(root)
print content(root.find('child2'))
结果是:
start here<child1>some text<sub1 />here</child1>and<child2>here as well<sub2 /><sub3 /></child2>end here
here as well<sub2 /><sub3 />
发布于 2018-07-02 00:13:51
这是基于其他解决方案,但其他解决方案在我的情况下不起作用(导致异常),而这个解决方案起作用:
from xml.etree import Element, ElementTree
def inner_xml(element: Element):
return (element.text or '') + ''.join(ElementTree.tostring(e, 'unicode') for e in element)
像在Mark Tolonen's answer中一样使用它。
https://stackoverflow.com/questions/3443831
复制相似问题