用于编辑 xml 标签中字符串的批处理脚本或 python 程序

发布于 2024-10-03 00:10:11 字数 154 浏览 0 评论 0 原文

我正在编写一个程序,用于搜索 xml 文档中的标签,并将标签之间的字符串从 localhost 更改为 manager。该标签可能会在xml文档中多次出现,并且文档确实有明确的路径。 python 还是 vbscript 对这个问题最有意义?谁能提供一个模板以便我可以开始使用?那太好了。谢谢。

I am looking to write a program that searches for the tags in an xml document and changes the string between the tags from localhost to manager. The tag might appear in the xml document multiple times, and the document does have a definite path. Would python or vbscript make the most sense for this problem? And can anyone provide a template so I can get started? That would be great. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉世弥音 2024-10-10 00:10:11

如果这是一件简单的事情,比如在这里或那里更改一些字符串,您可能可以使用 python 正则表达式完成所有操作,请检查此处:

对于更复杂的事情,我建议使用像 Beautiful Soup 这样的东西:

它有点过时,但包含您想要的所有内容曾经需要...

我同意这属于 stackoverflow.com,因为这是一个编程问题。

If it's a simple thing, like changing a few strings here and there, you might be able to do everything with a python regexp, check here:

For everything more complex, I would suggest using something like Beautiful Soup:

It's a bit outdated, but contains everything you would ever need...

I agree this belongs to stackoverflow.com, as it's a programming question.

娇俏 2024-10-10 00:10:11

我建议你直接去 python 的 lxml 库,不要回头。对 xml 的正则表达式操作可能会产生可怕的后果,而 BeautifulSoup 虽然相当流行,但已被正式放弃。

lxml 非常强大、快速且高效。对于您的任务,编写就足够了:

from lxml import etree
doc = etree.fromstring(content)
elements = doc.findall('tags_to_modify')
for el in elements:
    el.text = your_replacement_function(el.text)
print etree.tostring(doc)

您可以在 lxml 的文档中找到很多帮助:
http://lxml.de/

I suggest that you go straight to lxml library for python and don't look back. The regex manipulation of xml can have terrible consequences, and BeautifulSoup, although quite popular, is officially abandoned.

lxml is quite powerfull, fast and efficient. For your task, it is sufficient to write:

from lxml import etree
doc = etree.fromstring(content)
elements = doc.findall('tags_to_modify')
for el in elements:
    el.text = your_replacement_function(el.text)
print etree.tostring(doc)

You can find a lot of help in lxml's documentation:
http://lxml.de/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文