使用 Python 更改 XML 文件中包含的文本的最佳方法是什么? [英] What is the best way to change text contained in an XML file using Python?

查看:24
本文介绍了使用 Python 更改 XML 文件中包含的文本的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个名为MyData.xml"的现有普通 XML 文件,其中包含以下内容:

Let's say I have an existing trivial XML file named 'MyData.xml' that contains the following:

<?xml version="1.0" encoding="utf-8" ?>
<myElement>foo</myElement>

我想将foo"的文本值更改为bar",结果如下:

I want to change the text value of 'foo' to 'bar' resulting in the following:

<?xml version="1.0" encoding="utf-8" ?>
<myElement>bar</myElement>

完成后,我想保存更改.

Once I am done, I want to save the changes.

完成这一切的最简单和最简单的方法是什么?

What is the easiest and simplest way to accomplish all this?

推荐答案

使用 Python 的 最小化

Use Python's minidom

基本上,您将采取以下步骤:

Basically you will take the following steps:

  1. 将 XML 数据读入 DOM 对象
  2. 使用DOM方法修改文档
  3. 将新的 DOM 对象保存到新的 XML 文档

在这个过程中,python 规范应该很好地握住你的手.

The python spec should hold your hand rather nicely though this process.

这篇关于使用 Python 更改 XML 文件中包含的文本的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆