【问题标题】:How to get the root node of an xml file in Python?如何在 Python 中获取 xml 文件的根节点?
【发布时间】:2009-03-26 19:19:53
【问题描述】:

基本上我正在使用:

从 xml.etree 导入 ElementTree 作为 ET

path = 'C:\cool.xml'
et = ET.parse ( path )

但我不确定如何从 et 获取根?

【问题讨论】:

    标签: python xml


    【解决方案1】:

    你可能想要:

    et.getroot()
    

    查看来自effbot site 的 ElementTree 官方文档。请注意,Python 2.5(第一个开箱即包含 ElementTree 的 Python 版本)使用 ElementTree 1.2,而不是最近的 1.3。差别不大,仅供参考。

    【讨论】:

      【解决方案2】:
      root = et.getroot()
      

      我会推荐使用 lxml.etree 而不是 xml.etree.ElementTree,因为 lxml 更快并且界面相同。

      【讨论】:

      • 语法上没有区别吗?
      【解决方案3】:
      root = et.getroot()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-28
        • 2018-02-22
        相关资源
        最近更新 更多