在Python中获取xml标签中的所有嵌套子元素

2024-06-02 06:59:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个xml.etree.ElementTree具有以下内容的对象。在

<html>
 <body>
  <c>
   <winforms>
    <type-conversion>
     <opacity>
     </opacity>
    </type-conversion>
   </winforms>
  </c>
 </body>
</html>
<html>
 <body>
  <css>
   <css3>
    <internet-explorer-7>
    </internet-explorer-7>
   </css3>
  </css>
 </body>
</html>
<html>
 <body>
  <c>
   <code-generation>
    <j>
     <visualj>
     </visualj>
    </j>
   </code-generation>
  </c>
 </body>
</html>

我想得到每个body标记对中的所有标记。 例如,我希望上面示例的输出是:

^{pr2}$

如何在python中使用beauthoulsoup或ElementTree XML API来实现这一点?在


Tags: 标记htmltypecodebodycssinternetgeneration
1条回答
网友
1楼 · 发布于 2024-06-02 06:59:18

首先,XML规范只允许文档中有一个根元素。如果这是实际的XML,那么在解析之前需要用一个临时根元素包装它。在

现在,有了格式良好的XML,您可以使用xml.etree进行解析,并使用简单的XPath表达式.//body//*来查询<body>元素中的所有元素,无论是直接子元素还是嵌套元素:

from xml.etree import ElementTree as et

raw = '''xml string as posted in the question'''
root = et.fromstring('<root>'+raw+'</root>')

target_elements = root.findall('.//body/*')

result = [t.tag for t in target_elements]
print result
# output :
# ['c', 'winforms', 'type-conversion', 'opacity', 'css', 'css3', 'internet-explorer-7', 'c', 'code-generation', 'j', 'visualj']

相关问题 更多 >