How to make XML formatted using batch script tag by tag(Pretty Print)?











up vote
0
down vote

favorite












Suppose I am having below format in one file:



         `<xml><tag><othertag>ABC</othertag></tag></xml>`


But I need below formatted output :



<xml>
<tag>
<othertag>ABC</othertag>
</tag>




This formatted output i need it in other file.



help me with the batch script which will be able to do such formatting.










share|improve this question
























  • this can be done easy with xslt transformation and a script using vbscript,jscript or powershell (which are installed by default on windows) called by a batch script. Later I'll try to create a script for this.
    – npocmaka
    Nov 10 at 18:22










  • Working with XML in batch can be very tricky, due to all the special characters encountered. Search for xml formatter on google and you should find plenty of free software that can convert between deflated and pretty formatted forms.
    – jwdonahue
    Nov 10 at 19:28






  • 2




    Possible duplicate of Using batch code to mass xml formatting?
    – jwdonahue
    Nov 10 at 19:32










  • Take the tour, read How to Ask, and Minimal, Complete, and Verifiable example.
    – jwdonahue
    Nov 10 at 19:34















up vote
0
down vote

favorite












Suppose I am having below format in one file:



         `<xml><tag><othertag>ABC</othertag></tag></xml>`


But I need below formatted output :



<xml>
<tag>
<othertag>ABC</othertag>
</tag>




This formatted output i need it in other file.



help me with the batch script which will be able to do such formatting.










share|improve this question
























  • this can be done easy with xslt transformation and a script using vbscript,jscript or powershell (which are installed by default on windows) called by a batch script. Later I'll try to create a script for this.
    – npocmaka
    Nov 10 at 18:22










  • Working with XML in batch can be very tricky, due to all the special characters encountered. Search for xml formatter on google and you should find plenty of free software that can convert between deflated and pretty formatted forms.
    – jwdonahue
    Nov 10 at 19:28






  • 2




    Possible duplicate of Using batch code to mass xml formatting?
    – jwdonahue
    Nov 10 at 19:32










  • Take the tour, read How to Ask, and Minimal, Complete, and Verifiable example.
    – jwdonahue
    Nov 10 at 19:34













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Suppose I am having below format in one file:



         `<xml><tag><othertag>ABC</othertag></tag></xml>`


But I need below formatted output :



<xml>
<tag>
<othertag>ABC</othertag>
</tag>




This formatted output i need it in other file.



help me with the batch script which will be able to do such formatting.










share|improve this question















Suppose I am having below format in one file:



         `<xml><tag><othertag>ABC</othertag></tag></xml>`


But I need below formatted output :



<xml>
<tag>
<othertag>ABC</othertag>
</tag>




This formatted output i need it in other file.



help me with the batch script which will be able to do such formatting.







xml batch-file formatting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 13:58









LotPings

15.6k61531




15.6k61531










asked Nov 10 at 17:26









umesh yadav

91




91












  • this can be done easy with xslt transformation and a script using vbscript,jscript or powershell (which are installed by default on windows) called by a batch script. Later I'll try to create a script for this.
    – npocmaka
    Nov 10 at 18:22










  • Working with XML in batch can be very tricky, due to all the special characters encountered. Search for xml formatter on google and you should find plenty of free software that can convert between deflated and pretty formatted forms.
    – jwdonahue
    Nov 10 at 19:28






  • 2




    Possible duplicate of Using batch code to mass xml formatting?
    – jwdonahue
    Nov 10 at 19:32










  • Take the tour, read How to Ask, and Minimal, Complete, and Verifiable example.
    – jwdonahue
    Nov 10 at 19:34


















  • this can be done easy with xslt transformation and a script using vbscript,jscript or powershell (which are installed by default on windows) called by a batch script. Later I'll try to create a script for this.
    – npocmaka
    Nov 10 at 18:22










  • Working with XML in batch can be very tricky, due to all the special characters encountered. Search for xml formatter on google and you should find plenty of free software that can convert between deflated and pretty formatted forms.
    – jwdonahue
    Nov 10 at 19:28






  • 2




    Possible duplicate of Using batch code to mass xml formatting?
    – jwdonahue
    Nov 10 at 19:32










  • Take the tour, read How to Ask, and Minimal, Complete, and Verifiable example.
    – jwdonahue
    Nov 10 at 19:34
















this can be done easy with xslt transformation and a script using vbscript,jscript or powershell (which are installed by default on windows) called by a batch script. Later I'll try to create a script for this.
– npocmaka
Nov 10 at 18:22




this can be done easy with xslt transformation and a script using vbscript,jscript or powershell (which are installed by default on windows) called by a batch script. Later I'll try to create a script for this.
– npocmaka
Nov 10 at 18:22












Working with XML in batch can be very tricky, due to all the special characters encountered. Search for xml formatter on google and you should find plenty of free software that can convert between deflated and pretty formatted forms.
– jwdonahue
Nov 10 at 19:28




Working with XML in batch can be very tricky, due to all the special characters encountered. Search for xml formatter on google and you should find plenty of free software that can convert between deflated and pretty formatted forms.
– jwdonahue
Nov 10 at 19:28




2




2




Possible duplicate of Using batch code to mass xml formatting?
– jwdonahue
Nov 10 at 19:32




Possible duplicate of Using batch code to mass xml formatting?
– jwdonahue
Nov 10 at 19:32












Take the tour, read How to Ask, and Minimal, Complete, and Verifiable example.
– jwdonahue
Nov 10 at 19:34




Take the tour, read How to Ask, and Minimal, Complete, and Verifiable example.
– jwdonahue
Nov 10 at 19:34












2 Answers
2






active

oldest

votes

















up vote
0
down vote













try this (it does not check if the xml has valid syntax):



call ::beautifyXml "c:some.xml"
call ::beautifyXml "c:some.xml" > "c:new.xml"

exit /b %errorlevel%


:beautifyXml
powershell "function fx($xml, $i=2){$SW=New-Object System.IO.StringWriter;$XW=New-Object System.XMl.XmlTextWriter $SW; $XW.Formatting='indented';$XW.Indentation=$i;([xml]$xml).WriteContentTo($XW);$XW.Flush();$SW.Flush();Write-Output $SW.ToString();};FX (gc -path """%~f1""") -i 4"
goto :eof


if you the new file looks ok you can add additional move command to replace the old one.






share|improve this answer



















  • 1




    With XML files mostly encoded in UTF8 I'd prefer to write to file in the powershell part probaply with an -Encoding parameter.
    – LotPings
    Nov 11 at 13:57










  • @LotPings - Good point. Later (if I have the time) I'll try to edit the script.
    – npocmaka
    Nov 11 at 14:06


















up vote
0
down vote













A XML file should not be processed via a Batch file. However, this particular example give me the opportunity to get some fun with a Batch file, and it works! ;)



@echo off
setlocal EnableDelayedExpansion

for /F "tokens=2 delims=`" %%a in (input.txt) do set "format=%%a"

for /F %%a in ('copy /Z "%~F0" NUL') do set NL=%%a^
% Don't remove %
% these lines %
set "SP= "
set "format=%format:></=>^!NL^!^!SP:~0,-8^!" ^& set "SP=^!SP:~0,-4^!" ^& set /P "=</%"
< NUL (set /P "=%format:><=>!NL!!SP!" & set "SP= !SP!" & set /P "=<%" & echo/) > output.txt


Output:



<xml>
<tag>
<othertag>ABC</othertag>
</tag>
</xml>


For a description on the method used, see this thread.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241546%2fhow-to-make-xml-formatted-using-batch-script-tag-by-tagpretty-print%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    try this (it does not check if the xml has valid syntax):



    call ::beautifyXml "c:some.xml"
    call ::beautifyXml "c:some.xml" > "c:new.xml"

    exit /b %errorlevel%


    :beautifyXml
    powershell "function fx($xml, $i=2){$SW=New-Object System.IO.StringWriter;$XW=New-Object System.XMl.XmlTextWriter $SW; $XW.Formatting='indented';$XW.Indentation=$i;([xml]$xml).WriteContentTo($XW);$XW.Flush();$SW.Flush();Write-Output $SW.ToString();};FX (gc -path """%~f1""") -i 4"
    goto :eof


    if you the new file looks ok you can add additional move command to replace the old one.






    share|improve this answer



















    • 1




      With XML files mostly encoded in UTF8 I'd prefer to write to file in the powershell part probaply with an -Encoding parameter.
      – LotPings
      Nov 11 at 13:57










    • @LotPings - Good point. Later (if I have the time) I'll try to edit the script.
      – npocmaka
      Nov 11 at 14:06















    up vote
    0
    down vote













    try this (it does not check if the xml has valid syntax):



    call ::beautifyXml "c:some.xml"
    call ::beautifyXml "c:some.xml" > "c:new.xml"

    exit /b %errorlevel%


    :beautifyXml
    powershell "function fx($xml, $i=2){$SW=New-Object System.IO.StringWriter;$XW=New-Object System.XMl.XmlTextWriter $SW; $XW.Formatting='indented';$XW.Indentation=$i;([xml]$xml).WriteContentTo($XW);$XW.Flush();$SW.Flush();Write-Output $SW.ToString();};FX (gc -path """%~f1""") -i 4"
    goto :eof


    if you the new file looks ok you can add additional move command to replace the old one.






    share|improve this answer



















    • 1




      With XML files mostly encoded in UTF8 I'd prefer to write to file in the powershell part probaply with an -Encoding parameter.
      – LotPings
      Nov 11 at 13:57










    • @LotPings - Good point. Later (if I have the time) I'll try to edit the script.
      – npocmaka
      Nov 11 at 14:06













    up vote
    0
    down vote










    up vote
    0
    down vote









    try this (it does not check if the xml has valid syntax):



    call ::beautifyXml "c:some.xml"
    call ::beautifyXml "c:some.xml" > "c:new.xml"

    exit /b %errorlevel%


    :beautifyXml
    powershell "function fx($xml, $i=2){$SW=New-Object System.IO.StringWriter;$XW=New-Object System.XMl.XmlTextWriter $SW; $XW.Formatting='indented';$XW.Indentation=$i;([xml]$xml).WriteContentTo($XW);$XW.Flush();$SW.Flush();Write-Output $SW.ToString();};FX (gc -path """%~f1""") -i 4"
    goto :eof


    if you the new file looks ok you can add additional move command to replace the old one.






    share|improve this answer














    try this (it does not check if the xml has valid syntax):



    call ::beautifyXml "c:some.xml"
    call ::beautifyXml "c:some.xml" > "c:new.xml"

    exit /b %errorlevel%


    :beautifyXml
    powershell "function fx($xml, $i=2){$SW=New-Object System.IO.StringWriter;$XW=New-Object System.XMl.XmlTextWriter $SW; $XW.Formatting='indented';$XW.Indentation=$i;([xml]$xml).WriteContentTo($XW);$XW.Flush();$SW.Flush();Write-Output $SW.ToString();};FX (gc -path """%~f1""") -i 4"
    goto :eof


    if you the new file looks ok you can add additional move command to replace the old one.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 10 at 21:33

























    answered Nov 10 at 21:20









    npocmaka

    41k1083125




    41k1083125








    • 1




      With XML files mostly encoded in UTF8 I'd prefer to write to file in the powershell part probaply with an -Encoding parameter.
      – LotPings
      Nov 11 at 13:57










    • @LotPings - Good point. Later (if I have the time) I'll try to edit the script.
      – npocmaka
      Nov 11 at 14:06














    • 1




      With XML files mostly encoded in UTF8 I'd prefer to write to file in the powershell part probaply with an -Encoding parameter.
      – LotPings
      Nov 11 at 13:57










    • @LotPings - Good point. Later (if I have the time) I'll try to edit the script.
      – npocmaka
      Nov 11 at 14:06








    1




    1




    With XML files mostly encoded in UTF8 I'd prefer to write to file in the powershell part probaply with an -Encoding parameter.
    – LotPings
    Nov 11 at 13:57




    With XML files mostly encoded in UTF8 I'd prefer to write to file in the powershell part probaply with an -Encoding parameter.
    – LotPings
    Nov 11 at 13:57












    @LotPings - Good point. Later (if I have the time) I'll try to edit the script.
    – npocmaka
    Nov 11 at 14:06




    @LotPings - Good point. Later (if I have the time) I'll try to edit the script.
    – npocmaka
    Nov 11 at 14:06












    up vote
    0
    down vote













    A XML file should not be processed via a Batch file. However, this particular example give me the opportunity to get some fun with a Batch file, and it works! ;)



    @echo off
    setlocal EnableDelayedExpansion

    for /F "tokens=2 delims=`" %%a in (input.txt) do set "format=%%a"

    for /F %%a in ('copy /Z "%~F0" NUL') do set NL=%%a^
    % Don't remove %
    % these lines %
    set "SP= "
    set "format=%format:></=>^!NL^!^!SP:~0,-8^!" ^& set "SP=^!SP:~0,-4^!" ^& set /P "=</%"
    < NUL (set /P "=%format:><=>!NL!!SP!" & set "SP= !SP!" & set /P "=<%" & echo/) > output.txt


    Output:



    <xml>
    <tag>
    <othertag>ABC</othertag>
    </tag>
    </xml>


    For a description on the method used, see this thread.






    share|improve this answer

























      up vote
      0
      down vote













      A XML file should not be processed via a Batch file. However, this particular example give me the opportunity to get some fun with a Batch file, and it works! ;)



      @echo off
      setlocal EnableDelayedExpansion

      for /F "tokens=2 delims=`" %%a in (input.txt) do set "format=%%a"

      for /F %%a in ('copy /Z "%~F0" NUL') do set NL=%%a^
      % Don't remove %
      % these lines %
      set "SP= "
      set "format=%format:></=>^!NL^!^!SP:~0,-8^!" ^& set "SP=^!SP:~0,-4^!" ^& set /P "=</%"
      < NUL (set /P "=%format:><=>!NL!!SP!" & set "SP= !SP!" & set /P "=<%" & echo/) > output.txt


      Output:



      <xml>
      <tag>
      <othertag>ABC</othertag>
      </tag>
      </xml>


      For a description on the method used, see this thread.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        A XML file should not be processed via a Batch file. However, this particular example give me the opportunity to get some fun with a Batch file, and it works! ;)



        @echo off
        setlocal EnableDelayedExpansion

        for /F "tokens=2 delims=`" %%a in (input.txt) do set "format=%%a"

        for /F %%a in ('copy /Z "%~F0" NUL') do set NL=%%a^
        % Don't remove %
        % these lines %
        set "SP= "
        set "format=%format:></=>^!NL^!^!SP:~0,-8^!" ^& set "SP=^!SP:~0,-4^!" ^& set /P "=</%"
        < NUL (set /P "=%format:><=>!NL!!SP!" & set "SP= !SP!" & set /P "=<%" & echo/) > output.txt


        Output:



        <xml>
        <tag>
        <othertag>ABC</othertag>
        </tag>
        </xml>


        For a description on the method used, see this thread.






        share|improve this answer












        A XML file should not be processed via a Batch file. However, this particular example give me the opportunity to get some fun with a Batch file, and it works! ;)



        @echo off
        setlocal EnableDelayedExpansion

        for /F "tokens=2 delims=`" %%a in (input.txt) do set "format=%%a"

        for /F %%a in ('copy /Z "%~F0" NUL') do set NL=%%a^
        % Don't remove %
        % these lines %
        set "SP= "
        set "format=%format:></=>^!NL^!^!SP:~0,-8^!" ^& set "SP=^!SP:~0,-4^!" ^& set /P "=</%"
        < NUL (set /P "=%format:><=>!NL!!SP!" & set "SP= !SP!" & set /P "=<%" & echo/) > output.txt


        Output:



        <xml>
        <tag>
        <othertag>ABC</othertag>
        </tag>
        </xml>


        For a description on the method used, see this thread.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 17:22









        Aacini

        50.4k75173




        50.4k75173






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241546%2fhow-to-make-xml-formatted-using-batch-script-tag-by-tagpretty-print%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values