Can any one help me by suggesting a batch script to find 'utf-16' string in multiple xml files in a directory and replace it with 'utf-8'. With out using find and replace tools. The replacement need to be done in existing file itself.
1 Answer
Use any XSLT-processor. For example msxsl.
Command Line Transformation Utility
MSXML 4.0 Service Pack 2 (Microsoft XML Core Services)
zero.xsl
- style sheet transform test.xml
to test2.xml
<xsl:output method="xml" encoding="UTF-8" />
convert xml to UTF-8.
Zeroxml test.xml
Zeroxml.cmd:
@echo off
@set name=%1
msxsl.exe %name% zero.xsl -o %name:~0,-4%2.xml
zero.xsl:
<!-- The Identity Transformation -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- <xsl:output method="html" encoding="UTF-8" indent="yes" omit-xml-declaration="no"/> -->
<xsl:output method="html" media-type="application/vnd.ms-excel" encoding="UTF-8" indent="yes" omit-xml-declaration="no"/>
<!-- <xsl:output omit-xml-declaration="no" indent="yes" encoding="UTF-8" method="html" /> -->
<!-- <xsl:output method="xml" media-type="application/vnd.ms-excel" encoding="UTF-8" indent="yes" omit-xml-declaration="no"/> -->
<!-- <xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="no"/> -->
<!-- <xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/> -->
<!-- Whenever you match any node or any attribute -->
<xsl:template match="node()|@*">
<!-- Copy the current node -->
<xsl:copy>
<!-- Including any attributes it has and any child nodes -->
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*|@*|comment()|
processing-instruction()|text()">
<xsl:copy>
<xsl:apply-templates select="*|@*|comment()|
processing-instruction()|text()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Way2, convert UTF-16 to UTF-8 command line:
Unicode - UTF-16 format little-endian byte order.
powershell gc test.xml -encoding Unicode^|sc testUTF8.xml -encoding UTF8
BigEndianUnicode - UTF-16 format big-endian byte order.
powershell gc test.xml -encoding BigEndianUnicode^|sc testUTF8.xml -encoding UTF8
Convert all xml file UTF-16 in SourceDirXML directory and subdirectory to UTF-8
powershell $in='C:\SourceDirXML';$out='C:\OutputUTF8XML\';ls -Fo -r $in -Fi *.xml^|%{(gc $_.FullName -encoding Unicode^|sc ($out+$_.Name) -encoding UTF8)}
Search and replace a string in multiple xml files (within a directory) with Windows CMD:
powershell $in='C:\SourceDirXML';$out='C:\OutputUTF8XML\';ls -Fo -r $in -Fi *.xml^|%{(gc $_.FullName^|%{$_ -replace 'oldstring','newstring'}^|sc ($out+$_.Name) -encoding UTF8)}
-
I guess all that code in zero.xsl is to get it to do it for many files? what code would do it for just one file? that's a lot of code just to change a literal string from one thing to another. (if that's what he wants to do)– barlopMar 10, 2015 at 9:47
-
@barlop zero.xsl shows the transformation that does nothing with the text. Do the same for multiple files, you just need to loop
FOR /R %%i IN (*.xml) DO
. Which lines you want to replace in the xml file, and where?– STTRMar 10, 2015 at 9:51 -
so all that code in zero.xsl is just to replace one string with another string?– barlopMar 10, 2015 at 10:37
-
@barlop No, it's an empty conversion. When the file is converted into itself but for example with UTF-8 encoding specified in the options. If you have indicated in the example exactly what you want to convert, you can write a specific string conversion. Because it will be different for elements and attributes for the complete line for the substring.– STTRMar 10, 2015 at 10:46
-
thanks, you should clarify that all that does nothing and you specify the conversion, you should even specify an example of it converting a specific string. Then it'll be more useful for people. Yor answer should be useful for people looking for the answer to the question, other than just the OP. interesting thing re powershell too you mention there. hopefully the OP will cliarify what he wants to convert. But in the meantime, or even regardless, give a more useful example (one that works, that others can use and adapt) so that your example isn't a completely empty shell.– barlopMar 10, 2015 at 10:47
utf-16
and replace withutf-8
. If the OP wants to change encoding I don't think thats possible from cmd.