【发布时间】: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