【问题标题】:Python POM parser replace textPython POM 解析器替换文本
【发布时间】:2023-03-29 14:44:02
【问题描述】:

xml enter image description here

我需要更改屏幕上标记的版本。

我试试:

with open(PathPom,'r') as f:

    tree = ET.parse(f)
    root = tree.getroot()

    for elem in root.getiterator():
        try:
        elem.text = elem.text.replace('1.4.1', '5.0.0')
        except AttributeError:
        pass

tree.write(PathPom,xml_declaration=True, method='xml')

pom中所有1.4.1版本都是replace,文档中所有行以<ns0开头

你知道如何解决吗?

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 [http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>cterminal</groupId>
    <artifactId>Monitor</artifactId>
    <version>1.4.1</version>
    <packaging>lba</packaging>
    <properties>
        <lb.middlewareVersion>1.7.0</lb.middlewareVersion>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>.maven</groupId>
                <artifactId>lba-maven-plugin</artifactId>
                <version>${lb.middlewareVersion}</version>
                <extensions>true</extensions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-enforcer-plugin</artifactId>
                <version>1.4.1</version>
                <executions>
                    <execution>
                        <id>enforce-maven</id>
                        <goals>
                            <goal>enforce</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <requireMavenVersion>
                                    <message>The lba-maven-plugin requires Maven version to be at
                                        least 3.1.1!</message>
                                    <version>[3.1.1,)</version>
                                </requireMavenVersion>
                                <dependencyConvergence/>
                            </rules>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

【问题讨论】:

  • 欢迎来到 SO!请将您的 XML 发布为代码格式的文本(而不是图像),最好简化为仅包含相关元素、目标 version 和一些其他元素以演示问题,即 1 或 2 个元素被您的代码替换,而它不应该...
  • 另一个提示:要格式化你的代码,选择代码块然后点击编辑器顶部的{}或按住CTRL+K

标签: xml python-2.7 pom.xml


【解决方案1】:

为防止ElementTree 添加ns0 前缀,您需要在解析XML 之前将默认命名空间映射到空前缀。由于只有一个元素需要更新,您可以使用 find() 轻松获取该元素:

....
ET.register_namespace('', 'http://maven.apache.org/POM/4.0.0')
tree = ET.parse(f)
root = tree.getroot()

version = root.find('{http://maven.apache.org/POM/4.0.0}version')
version.text = '5.0.0'
....

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 2020-04-18
    • 2018-10-17
    • 2019-07-05
    • 2011-10-01
    相关资源
    最近更新 更多