我在尝试在xml文件中搜索此xml文件中所有标记内的文本时遇到了问题。 python脚本需要能够将每个版本标记的最后一个数字增加1,例如2.22.0.4变为2.22.0.5
有人能给我一个执行此任务所需的python代码类型的大纲吗?
这是xml:
<build>
<defaultGoal>deploy</defaultGoal>
<plugins>
<plugin>
<groupId>mygroupid</groupId>
<artifactId>my_plugin</artifactId>
<version>9.9</version>
<executions>
<execution>
<id>unpack</id>
<phase>process</phase>
<goals>
<goal>unpack</goal>
</goals>
</execution>
</executions>
<configuration>
<artifactItems>
<artifactItem>
<groupId>mygroupid</groupId>
<artifactId>myfile</artifactId>
<version>0.0.11.29</version>
<type>zip</type>
<outputDirectory>.</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>mygroupid</groupId>
<artifactId>myfile</artifactId>
<version>0.1.9.10</version>
<type>zip</type>
<outputDirectory>.</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>mygroupid</groupId>
<artifactId>myfile</artifactId>
<version>0.19.9.10</version>
<type>zip</type>
<outputDirectory>.</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>mygroupid</groupId>
<artifactId>myfile</artifactId>
<version>0.1.9.9</version>
<type>zip</type>
<outputDirectory>.</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>mygroupid</groupId>
<artifactId>myfile</artifactId>
<version>0.1.9.10</version>
<type>zip</type>
<outputDirectory>.</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>mygroupid</groupId>
<artifactId>myfile</artifactId>
<version>0.5.9.10</version>
<type>zip</type>
<outputDirectory>.</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>mygroupid</groupId>
<artifactId>myfile</artifactId>
<version>0.15.29.10</version>
<type>zip</type>
<outputDirectory>.</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</plugin>
</plugins>
答案 0 :(得分:1)
如果您的目标节点仅为version
,您只需循环浏览tree
并动态编辑text
属性,就像这样(我使用lxml
)在我的示例代码中,其他库应该非常相似:
In [1]: import lxml.etree as et
In [2]: root = et.parse("sample.xml")
In [3]: tree = root.getroot()
In [4]: for node in tree.iter("version"):
...: v = node.text
...: v = v.split(".")
...: if len(v) > 1: # make sure version does exist in node
...: node.text = '.'.join(v[:-1]) + '.' + str(int(v[-1]) + 1)
...:
In [5]: print et.tostring(tree)
<build>
<defaultGoal>deploy</defaultGoal>
<plugins>
<plugin>
<groupId>mygroupid</groupId>
<artifactId>my_plugin</artifactId>
<version>9.10</version>
<executions>
<execution>
<id>unpack</id>
<phase>process</phase>
<goals>
<goal>unpack</goal>
</goals>
</execution>
</executions>
<configuration>
<artifactItems>
<artifactItem>
<groupId>mygroupid</groupId>
<artifactId>myfile</artifactId>
<version>0.0.11.30</version>
<type>zip</type>
<outputDirectory>.</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>mygroupid</groupId>
<artifactId>myfile</artifactId>
<version>0.1.9.11</version>
<type>zip</type>
<outputDirectory>.</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>mygroupid</groupId>
<artifactId>myfile</artifactId>
<version>0.19.9.11</version>
<type>zip</type>
<outputDirectory>.</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>mygroupid</groupId>
<artifactId>myfile</artifactId>
<version>0.1.9.10</version>
<type>zip</type>
<outputDirectory>.</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>mygroupid</groupId>
<artifactId>myfile</artifactId>
<version>0.1.9.11</version>
<type>zip</type>
<outputDirectory>.</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>mygroupid</groupId>
<artifactId>myfile</artifactId>
<version>0.5.9.11</version>
<type>zip</type>
<outputDirectory>.</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>mygroupid</groupId>
<artifactId>myfile</artifactId>
<version>0.15.29.11</version>
<type>zip</type>
<outputDirectory>.</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</plugin>
</plugins>
</build>
找到version
个节点后,只需将.text
属性分配给新值 - 我只需将其分割为.
,然后递增最后一个部分,然后再加入它们。
希望这有帮助。