【问题标题】:Parsing Dependencies Pom File解析依赖项 Pom 文件
【发布时间】:2020-11-07 22:23:15
【问题描述】:

我正在尝试解析一个 pom 文件,但我遇到了一个我无法找到解决方案的问题。我当前的代码成功地从 pom 文件读取、解析和输出。问题在于依赖项没有按相同顺序指定 artifactId、groupId、version。

我应该在我的 for 循环中放置什么条件,以便它忽略标签(例如 type)并且只检索 artifactId、groupId 和版本?

代码:

for dep in depend:
    infoList = []
    counter += 1
    for child in dep.getchildren():
        infoList.append(child.tag.split('}')[1])
        infoList.append(child.text)

    #list where data is being stored
    dependencyInfo[infoList[1]].update({infoList[2] : infoList[3],infoList[4] : infoList[5]})

Pom 文件示例

<dependency>
    <artifactId>slf4j-api</artifactId>
    <groupId>org.slf4j</groupId>
    <type>jar</type>
    <version>1.6.1</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>log4j-over-slf4j</artifactId>
    <version>1.6.1</version>
</dependency>
<dependency>
    <groupId>sample.ProjectA</groupId>
    <artifactId>Project-A</artifactId>
    <scope>compile</scope>
    <version>1.0</version>
    <optional>true</optional>
</dependency>

实际输出:

defaultdict(<class 'dict'>,{'slf4j-api': {'groupId': 'org.slf4j', 'type': 'jar'}, 'org.slf4j': {'artifactId': 'log4j-over-slf4j', 'version': '1.6.1'}, 'sample.ProjectA': {'artifactId': 'Project-A', 'scope': 'compile'}})

预期输出:

defaultdict(<class 'dict'>,{'slf4j-api': {'groupId': 'org.slf4j', 'version': '1.6.1'}, 'org.slf4j': {'artifactId': 'log4j-over-slf4j', 'version': '1.6.1'}, 'sample.ProjectA': {'artifactId': 'Project-A', 'version': '1.0'}})

任何帮助将不胜感激

【问题讨论】:

    标签: python arrays dictionary parsing pom.xml


    【解决方案1】:

    由于您的文件示例看起来像 XML,我建议使用 XML parser 而不是自己制作。

    获取所需的确切数据有一点学习曲线,但值得学习,因为它可以扩展以解析更高级和更复杂的类型,并且不会出现逻辑错误。

    【讨论】:

      猜你喜欢
      • 2013-05-29
      • 2014-11-29
      • 2019-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多