好的,所以我对使用 For/F 不是很熟悉。如果文件是静态的并且有一组我可以跳过然后从中提取数据的行,我可以修改它。我目前正在尝试修改 .XML 文件。该文件将具有不同的行数,但始终具有以下内容
</SyncWindow>
</AutoSyncWindows>
<SyncServiceConnections />
<DaysToRetainRecordedData>90</DaysToRetainRecordedData>
<SyncRestartRequired>false</SyncRestartRequired>
- <LastGroupsSynced>
<DaysToRetainRecordedData>90</DaysToRetainRecordedData>
的值可能不同,例如 <DaysToRetainRecordedData>30</DaysToRetainRecordedData>
使用标记,什么是最有效的方法来搜索该行的 .XML 文件并用以下 <DaysToRetainRecordedData>0</DaysToRetainRecordedData>
覆盖它
我无法覆盖整个 .XML 文件,因为它们具有因机器而异的唯一服务器 key 。因此我需要能够找到该行并将值编辑为 0。 有什么想法吗?如果 For/F 不是最有效的方法,我可以根据需要转到 VBS。但它必须从纯 shellcode 调用,并且会使事情变得有点复杂。
最佳答案
最优雅、灵活和安全的方式是下载 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>
在命令行中调用它:
msxsl.exe input.xml DaysToRetainRecordedData.xsl –o output.xml newValue=0
命令行参数 newValue
将出现在 XSL 程序中。
关于xml - 批量修改XML文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4661650/