windows 批量修改XML文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4661650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Modify XML file in batch
提问by Zer0mod
Ok, so I'm not super familiar with using For /F. I can modify it if the file is static and has a set ammount of lines that i can skip and then pull data from. I'm currently trying to modify an .XML file. The file will have varying ammounts of lines, but will always have the following
好的,所以我对使用 For /F 不是很熟悉。如果文件是静态的并且有一组我可以跳过然后从中提取数据的行,我可以修改它。我目前正在尝试修改 .XML 文件。该文件将具有不同的行数,但始终具有以下内容
</SyncWindow>
</AutoSyncWindows>
<SyncServiceConnections />
<DaysToRetainRecordedData>90</DaysToRetainRecordedData>
<SyncRestartRequired>false</SyncRestartRequired>
- <LastGroupsSynced>
</SyncWindow>
</AutoSyncWindows>
<SyncServiceConnections />
<DaysToRetainRecordedData>90</DaysToRetainRecordedData>
<SyncRestartRequired>false</SyncRestartRequired>
- <LastGroupsSynced>
The value for <DaysToRetainRecordedData>90</DaysToRetainRecordedData>
may differ, for example <DaysToRetainRecordedData>30</DaysToRetainRecordedData>
的值<DaysToRetainRecordedData>90</DaysToRetainRecordedData>
可能不同,例如<DaysToRetainRecordedData>30</DaysToRetainRecordedData>
Using tokens, what would be the most efficient way to search that .XML file for that line and overwrite it with the following <DaysToRetainRecordedData>0</DaysToRetainRecordedData>
使用令牌,搜索该行的 .XML 文件并用以下内容覆盖它的最有效方法是什么 <DaysToRetainRecordedData>0</DaysToRetainRecordedData>
I can not overwrite the entire .XML file as they have unique server keys that will vary from machine to machine.So I need to be able to find that line and edit the value to 0. Any thoughts? If For /F is not the most efficient way to go, I can move to VBS if need be. But it would have to be called from pure shellcode and would make things a bit more complex.
我无法覆盖整个 .XML 文件,因为它们具有独特的服务器密钥,这些密钥会因机器而异。所以我需要能够找到该行并将值编辑为 0。有什么想法吗?如果 For /F 不是最有效的方法,如果需要,我可以转移到 VBS。但是它必须从纯 shellcode 中调用,并且会使事情变得更复杂一些。
采纳答案by Tomalak
The most elegant, flexible and safe way to do this would be to download msxsl.exeand then use a tiny XSLT stylesheet to modify only the value you want in the XML:
执行此操作的最优雅、灵活和安全的方法是下载msxsl.exe,然后使用一个很小的 XSLT 样式表仅修改您想要的 XML 值:
<!-- DaysToRetainRecordedData.xsl -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="newValue" select="0" />
<!-- this template copies your input XML unchanged -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<!-- this template changes one single value -->
<xsl:template match="DaysToRetainRecordedData/text()">
<xsl:value-of select="$newValue" />
</xsl:template>
</xsl:stylesheet>
Call that on the command line with:
在命令行上调用它:
msxsl.exe input.xml DaysToRetainRecordedData.xsl –o output.xml newValue=0
The command line parameter newValue
will show up in the XSL program.
命令行参数newValue
将显示在 XSL 程序中。