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

发布于 2024-10-03 00:24:23 字数 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技术交流群

发布评论

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

评论(5

清旖 2024-10-10 00:24:23

几乎所有语言都可以解决这个问题,包括Python和Vbscript。

不过,如果使用 Python 或其他具有大量 XML 处理库的语言编写脚本会更好。

如果你只是搜索标签,你可以使用 beautifulsoup。

You can solve this problem in almost all languages including Python and Vbscript.

How ever it will be nicer to have the script in python or other languages that have quite a number of XML processing libraries.

If you are just searching for tags, you can use beautifulsoup.

遇到 2024-10-10 00:24:23

我会为此使用 XSLT。如何调用 XSLT 取决于您,但 libxslt 附带了 xsltproc

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="//sometag">
    <sometag>manager</sometag>
  </xsl:template>
</xsl:stylesheet>

I'd use XSLT for this. How you invoke the XSLT is up to you, but libxslt comes with xsltproc.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="//sometag">
    <sometag>manager</sometag>
  </xsl:template>
</xsl:stylesheet>
很酷又爱笑 2024-10-10 00:24:23

使用 vbscript 还是 python 实际上是一个考虑到您所处的环境、您正在使用的系统以及您的公司/客户的要求的问题。

xml 文档模板可能会有所帮助,但作为个人偏好,我倾向于使用 Python 直接解析 xml。

我开始使用的一些有用的示例可以在这里找到: http ://www.xml.com/pub/a/2002/12/11/py-xml.html

虽然它们不能解决您的具体问题,但它们可能会帮助您入门。

Whether to use vbscript or python is really a question of what makes sense given the environment you're in, the systems you're working on, and the requirements of your company/client.

An xml document template may help either way, but I'd lean toward Python for parsing the xml directly, as a personal preference.

Some helpful examples I started with, can be found here: http://www.xml.com/pub/a/2002/12/11/py-xml.html

Though they don't address your specific problem, they might help get you started.

鼻尖触碰 2024-10-10 00:24:23

这是一个特定的 VB 示例,它完全符合您的要求。它可以轻松转换为 VBScript 并使用 MSXML2.DOMDocument COM 对象。 Un

Dim doc
Dim nlist
Dim node

Set doc = CreateObject("MSXML2.DOMDocument")
doc.setProperty "SelectionLanguage", "XPath"
doc.Load "c:\books.xml"
Set nlist = doc.selectNodes("//book/Title[contains(.,'localhost')]")
MsgBox "Matching Nodes : " & nlist.length

For Each node In nlist
  WScript.Echo node.nodeName & " : " & node.Text
Next

另一种方法可能是一种相当肮脏的方法,但它会起作用。您可以执行简单的字符串替换,替换“> localhost <”与“>经理<”。通过包含“>”和“<”字符,它将确保 XML 节点值恰好是“localhost”。

strXML = "<foo><bar>localhost</bar><bar2>localhost</bar></foo>"
WScript.echo Replace(strXML, ">localhost<", ">manager<")

Here's a specific VB example that does exactly what you are asking for. It can easily be converted into VBScript and uses the MSXML2.DOMDocument COM object. Un

Dim doc
Dim nlist
Dim node

Set doc = CreateObject("MSXML2.DOMDocument")
doc.setProperty "SelectionLanguage", "XPath"
doc.Load "c:\books.xml"
Set nlist = doc.selectNodes("//book/Title[contains(.,'localhost')]")
MsgBox "Matching Nodes : " & nlist.length

For Each node In nlist
  WScript.Echo node.nodeName & " : " & node.Text
Next

Another way to do it would be a rather dirty way, but it would work. You can perform a simple string replace, replacing ">localhost<" with ">manager<". by including the ">" and "<" characters, it would ensure the XML node value was exactly "localhost".

strXML = "<foo><bar>localhost</bar><bar2>localhost</bar></foo>"
WScript.echo Replace(strXML, ">localhost<", ">manager<")
画尸师 2024-10-10 00:24:23

我能够通过使用提供的 vbscript 解决方案来使其工作。我之前没有致力于 Visual Basic 脚本的原因是我认为不可能使用 PsExec 远程执行该脚本。事实证明我在服务器故障的帮助下也解决了这个问题。如果您对其工作原理感兴趣,cscript.exe 是 PsExec 的命令参数,而 vbscript 文件用作 cscript 的参数。感谢大家的帮助!

I was able to get this to work by using the vbscript solutions provided. The reasons I hadn't committed to a Visual Basic script before was that I didn't think it was possible to execute this script remotely with PsExec. It turns out I solved this problem as well with the help of Server Fault. In case you are interested in how that works, cscript.exe is the command parameter of PsExec and the vbscript file serves as the argument of cscript. Thanks for all the help, everyone!

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