How to make XML formatted using batch script tag by tag(Pretty Print)?
up vote
-1
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.
xml batch-file formatting
add a comment |
up vote
-1
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.
xml batch-file formatting
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
add a comment |
up vote
-1
down vote
favorite
up vote
-1
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.
xml batch-file formatting
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
xml batch-file formatting
edited Nov 11 at 13:58
LotPings
16.8k61531
16.8k61531
asked Nov 10 at 17:26
umesh yadav
71
71
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
add a comment |
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
add a comment |
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.
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
add a comment |
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.
HI Aacini, you have given an excellent solution !! , please can you give a fix for handling more than 8150 characters in your solution over this link stackoverflow.com/questions/33077104/…
– umesh yadav
Nov 26 at 20:29
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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.
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
add a comment |
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.
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
add a comment |
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.
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.
edited Nov 10 at 21:33
answered Nov 10 at 21:20
npocmaka
41.3k1184127
41.3k1184127
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
add a comment |
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
add a comment |
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.
HI Aacini, you have given an excellent solution !! , please can you give a fix for handling more than 8150 characters in your solution over this link stackoverflow.com/questions/33077104/…
– umesh yadav
Nov 26 at 20:29
add a comment |
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.
HI Aacini, you have given an excellent solution !! , please can you give a fix for handling more than 8150 characters in your solution over this link stackoverflow.com/questions/33077104/…
– umesh yadav
Nov 26 at 20:29
add a comment |
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.
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.
answered Nov 11 at 17:22
Aacini
50.8k75173
50.8k75173
HI Aacini, you have given an excellent solution !! , please can you give a fix for handling more than 8150 characters in your solution over this link stackoverflow.com/questions/33077104/…
– umesh yadav
Nov 26 at 20:29
add a comment |
HI Aacini, you have given an excellent solution !! , please can you give a fix for handling more than 8150 characters in your solution over this link stackoverflow.com/questions/33077104/…
– umesh yadav
Nov 26 at 20:29
HI Aacini, you have given an excellent solution !! , please can you give a fix for handling more than 8150 characters in your solution over this link stackoverflow.com/questions/33077104/…
– umesh yadav
Nov 26 at 20:29
HI Aacini, you have given an excellent solution !! , please can you give a fix for handling more than 8150 characters in your solution over this link stackoverflow.com/questions/33077104/…
– umesh yadav
Nov 26 at 20:29
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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