Python元素树查找在某些pom.xml中不返回任何内容

时间:2018-09-18 11:39:47

标签: python xml

我想使用在pom.xml中添加一些元素。我想使用查找来找到<dependencies>。但是当解析一些pom.xml文件时,它返回NONE。我比较了这些pom.xml,发现这些pom.xml文件不是以<?xml version='1.0' encoding='utf-8'?>开头,这是根本原因吗?我该如何处理这些pom.xml文件。

Python代码:
tree = Et.ElementTree() tree.parse("pom.xml") root = tree.getroot() dep_child = root.find("{http://maven.apache.org/POM/4.0.0}dependencies")

1 个答案:

答案 0 :(得分:0)

    dep_child = root.find(pre + "dependencies")
    if dep_child is None:
        logging.error("Cannot find dependencies element under the root object")
        dep_child = root.find("*//" + pre + "dependencies")
    if dep_child is None:
        logging.error("Cannot find dependencies element on pom.xml")
        return False

尝试其他参数