python操作xml

python操作xml

  1. pip3 install lxml
  2. 通过etree包的parse函数打开文件
  3. 获取到root节点
  4. 设置对应的ns
  5. 在根结点之后查找,并指定ns
  6. 对应元素的value改值
  7. tree.write(“filePath”)
from lxml import etree
tree=etree.parse("pom.xml")
root = tree.getroot()
namespaces = {'mvn': 'http://maven.apache.org/POM/4.0.0'}
key_to_modify = 'sharedLIB.version'
element = root.find(f".//mvn:properties/mvn:{key_to_modify}", namespaces)
if element is not None:
    new_value = 'new-value'
    element.text = new_value
    print(f"Value of '{key_to_modify}' changed to '{new_value}'")
else:
    print(f"Element '{key_to_modify}' not found")

# Save the modified XML to a new file
tree.write('pom.xml', encoding='utf-8', xml_declaration=True, pretty_print=True)
<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>
    <version>2.36.0</version>
    <parent>
        <groupId>com.sap.gtt.v2</groupId>
        <artifactId>gtt-v2-parent-pom</artifactId>
        <version>[2.37.0, 2.38.0)</version>
    </parent>
    <properties>
        <whitesource.projectName>LBN_FND_TTWRITE</whitesource.projectName>
        <vulas.ppms.component>SAP LBN 2.0</vulas.ppms.component>
        <vulas.ppms.objno.scv>xxxxxxx</vulas.ppms.objno.scv>
        <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
        <sharedLIB.version>[2.42.0, 2.43.0)</sharedLIB.version>
        <sonar.exclusions>
            **/writeservice/App.java,
            **/writeservice/configuration/*
        </sonar.exclusions>
    </properties>
    <artifactId>gtt-v2-core-inbound-writeservice</artifactId>
    <packaging>jar</packaging>
    <name>gtt-v2-core-inbound-writeservice</name>

    <dependencies>
        <dependency>
            <groupId>org.everit.json</groupId>
            <artifactId>org.everit.json.schema</artifactId>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>gtt-v2-shared-library</artifactId>
            <version>${sharedLIB.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值