Modifying an XML File in Batch Mode

Good, so I'm not very familiar with using the / F function. I can change it if the file is static and has a given number of lines that I can skip and then extract the data. I am currently trying to modify a .XML file. The file will have different numbers of lines, but will always have the following

</SyncWindow> </AutoSyncWindows> <SyncServiceConnections /> <DaysToRetainRecordedData>90</DaysToRetainRecordedData> <SyncRestartRequired>false</SyncRestartRequired> - <LastGroupsSynced>

The value of <DaysToRetainRecordedData>90</DaysToRetainRecordedData> may be different, for example <DaysToRetainRecordedData>30</DaysToRetainRecordedData>

Using tokens, what is the most efficient way to find this .XML file for this string and overwrite it with <DaysToRetainRecordedData>0</DaysToRetainRecordedData>

I cannot overwrite the entire .XML file, as they have unique server keys that will differ from machine to machine. So I need to find this line and change the value to 0. Any suggestions? If For / F is not the most efficient way, I can upgrade to VBS if necessary. But it will need to be called from pure shellcode and make things a little more complicated.

+4
source share
1 answer

The most elegant, flexible and safe way to do this is to download msxsl.exe , and then use the tiny XSLT stylesheet to change only the value you want in 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 this on the command line with

 msxsl.exe input.xml DaysToRetainRecordedData.xsl –o output.xml newValue=0 

The newValue command-line newValue will appear in the XSL program.

+4
source

Source: https://habr.com/ru/post/1335146/


All Articles