Batch-Script for formatting XML-files | Search for Strings and comment them out
up vote
0
down vote
favorite
So I have a XML-File, which isn't formatted, meaning it only has one line. The problem I have is, I want to delete two lines or comment them out.
Is there a way to format the XML-file using a batch-script, so if i have:
<xml><tag><othertag></othertag></tag></xml>
the output would be
<xml>
<tag>
<othertag>
</othertag>
</tag>
</xml>
Alternatively, how can I replace/add Strings, when I only have a pattern, and can't use findstr /v, because I only have one line?
Thanks in advance!
xml string batch-file format
add a comment |
up vote
0
down vote
favorite
So I have a XML-File, which isn't formatted, meaning it only has one line. The problem I have is, I want to delete two lines or comment them out.
Is there a way to format the XML-file using a batch-script, so if i have:
<xml><tag><othertag></othertag></tag></xml>
the output would be
<xml>
<tag>
<othertag>
</othertag>
</tag>
</xml>
Alternatively, how can I replace/add Strings, when I only have a pattern, and can't use findstr /v, because I only have one line?
Thanks in advance!
xml string batch-file format
Are you limited to batch as a scripting language ?
– Wald
Oct 12 '15 at 9:17
Unfortunately yes.
– user5417542
Oct 12 '15 at 9:29
2
Use xsl and an identity transform with indenting.
– Kevin Brown
Oct 12 '15 at 9:31
The thing is, I dont need to do this for only one file. I need to do this for many subdirectories. And I have not much knowledge about xsl.
– user5417542
Oct 12 '15 at 9:52
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
So I have a XML-File, which isn't formatted, meaning it only has one line. The problem I have is, I want to delete two lines or comment them out.
Is there a way to format the XML-file using a batch-script, so if i have:
<xml><tag><othertag></othertag></tag></xml>
the output would be
<xml>
<tag>
<othertag>
</othertag>
</tag>
</xml>
Alternatively, how can I replace/add Strings, when I only have a pattern, and can't use findstr /v, because I only have one line?
Thanks in advance!
xml string batch-file format
So I have a XML-File, which isn't formatted, meaning it only has one line. The problem I have is, I want to delete two lines or comment them out.
Is there a way to format the XML-file using a batch-script, so if i have:
<xml><tag><othertag></othertag></tag></xml>
the output would be
<xml>
<tag>
<othertag>
</othertag>
</tag>
</xml>
Alternatively, how can I replace/add Strings, when I only have a pattern, and can't use findstr /v, because I only have one line?
Thanks in advance!
xml string batch-file format
xml string batch-file format
asked Oct 12 '15 at 9:01
user5417542
5612726
5612726
Are you limited to batch as a scripting language ?
– Wald
Oct 12 '15 at 9:17
Unfortunately yes.
– user5417542
Oct 12 '15 at 9:29
2
Use xsl and an identity transform with indenting.
– Kevin Brown
Oct 12 '15 at 9:31
The thing is, I dont need to do this for only one file. I need to do this for many subdirectories. And I have not much knowledge about xsl.
– user5417542
Oct 12 '15 at 9:52
add a comment |
Are you limited to batch as a scripting language ?
– Wald
Oct 12 '15 at 9:17
Unfortunately yes.
– user5417542
Oct 12 '15 at 9:29
2
Use xsl and an identity transform with indenting.
– Kevin Brown
Oct 12 '15 at 9:31
The thing is, I dont need to do this for only one file. I need to do this for many subdirectories. And I have not much knowledge about xsl.
– user5417542
Oct 12 '15 at 9:52
Are you limited to batch as a scripting language ?
– Wald
Oct 12 '15 at 9:17
Are you limited to batch as a scripting language ?
– Wald
Oct 12 '15 at 9:17
Unfortunately yes.
– user5417542
Oct 12 '15 at 9:29
Unfortunately yes.
– user5417542
Oct 12 '15 at 9:29
2
2
Use xsl and an identity transform with indenting.
– Kevin Brown
Oct 12 '15 at 9:31
Use xsl and an identity transform with indenting.
– Kevin Brown
Oct 12 '15 at 9:31
The thing is, I dont need to do this for only one file. I need to do this for many subdirectories. And I have not much knowledge about xsl.
– user5417542
Oct 12 '15 at 9:52
The thing is, I dont need to do this for only one file. I need to do this for many subdirectories. And I have not much knowledge about xsl.
– user5417542
Oct 12 '15 at 9:52
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
@echo off
setlocal EnableDelayedExpansion
rem Create a variable with spaces
set "spaces= "
for /L %%i in (1,1,5) do set "spaces=!spaces!!spaces!"
rem Read the line
for /F "delims=" %%a in (input.txt) do set "line=%%a"
set level=0
:nextTag
rem Separate first tag from line
for /F "tokens=1* delims=<" %%a in ("!line!") do (
set "tag=%%a"
set "line=%%b"
)
rem Show first tag in one separate line
if "%tag:~0,1%" equ "/" set /A level-=5
echo !spaces:~0,%level%!^<!tag!
if "%tag:~0,1%" neq "/" set /A level+=5
if defined line goto nextTag
Output:
<xml>
<tag>
<othertag>
</othertag>
</tag>
</xml>
Note that the maximum length of a line that can be processed this way is 8150 characters approx. Also, this solution remove any exclamation-marks from the file. Both points can be fixed, if required.
Great! On the commandline it works, only need to add, that it writes the output in a new file. The character-limitation is no problem, but they exclamation-mark would be a deal, since the files will be used for mocking, so there should be no change.
– user5417542
Oct 12 '15 at 13:15
add a comment |
up vote
0
down vote
try this :
@echo off
call ::beautifyXml c:some.xml
exit /b %errorlevel%
::::::::::::::::::
:beautifyXml
@echo off
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">"%~dp1~"
move /y "%~dp1~" "%~f1" >nul 2>nul
goto :eof
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
@echo off
setlocal EnableDelayedExpansion
rem Create a variable with spaces
set "spaces= "
for /L %%i in (1,1,5) do set "spaces=!spaces!!spaces!"
rem Read the line
for /F "delims=" %%a in (input.txt) do set "line=%%a"
set level=0
:nextTag
rem Separate first tag from line
for /F "tokens=1* delims=<" %%a in ("!line!") do (
set "tag=%%a"
set "line=%%b"
)
rem Show first tag in one separate line
if "%tag:~0,1%" equ "/" set /A level-=5
echo !spaces:~0,%level%!^<!tag!
if "%tag:~0,1%" neq "/" set /A level+=5
if defined line goto nextTag
Output:
<xml>
<tag>
<othertag>
</othertag>
</tag>
</xml>
Note that the maximum length of a line that can be processed this way is 8150 characters approx. Also, this solution remove any exclamation-marks from the file. Both points can be fixed, if required.
Great! On the commandline it works, only need to add, that it writes the output in a new file. The character-limitation is no problem, but they exclamation-mark would be a deal, since the files will be used for mocking, so there should be no change.
– user5417542
Oct 12 '15 at 13:15
add a comment |
up vote
1
down vote
accepted
@echo off
setlocal EnableDelayedExpansion
rem Create a variable with spaces
set "spaces= "
for /L %%i in (1,1,5) do set "spaces=!spaces!!spaces!"
rem Read the line
for /F "delims=" %%a in (input.txt) do set "line=%%a"
set level=0
:nextTag
rem Separate first tag from line
for /F "tokens=1* delims=<" %%a in ("!line!") do (
set "tag=%%a"
set "line=%%b"
)
rem Show first tag in one separate line
if "%tag:~0,1%" equ "/" set /A level-=5
echo !spaces:~0,%level%!^<!tag!
if "%tag:~0,1%" neq "/" set /A level+=5
if defined line goto nextTag
Output:
<xml>
<tag>
<othertag>
</othertag>
</tag>
</xml>
Note that the maximum length of a line that can be processed this way is 8150 characters approx. Also, this solution remove any exclamation-marks from the file. Both points can be fixed, if required.
Great! On the commandline it works, only need to add, that it writes the output in a new file. The character-limitation is no problem, but they exclamation-mark would be a deal, since the files will be used for mocking, so there should be no change.
– user5417542
Oct 12 '15 at 13:15
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
@echo off
setlocal EnableDelayedExpansion
rem Create a variable with spaces
set "spaces= "
for /L %%i in (1,1,5) do set "spaces=!spaces!!spaces!"
rem Read the line
for /F "delims=" %%a in (input.txt) do set "line=%%a"
set level=0
:nextTag
rem Separate first tag from line
for /F "tokens=1* delims=<" %%a in ("!line!") do (
set "tag=%%a"
set "line=%%b"
)
rem Show first tag in one separate line
if "%tag:~0,1%" equ "/" set /A level-=5
echo !spaces:~0,%level%!^<!tag!
if "%tag:~0,1%" neq "/" set /A level+=5
if defined line goto nextTag
Output:
<xml>
<tag>
<othertag>
</othertag>
</tag>
</xml>
Note that the maximum length of a line that can be processed this way is 8150 characters approx. Also, this solution remove any exclamation-marks from the file. Both points can be fixed, if required.
@echo off
setlocal EnableDelayedExpansion
rem Create a variable with spaces
set "spaces= "
for /L %%i in (1,1,5) do set "spaces=!spaces!!spaces!"
rem Read the line
for /F "delims=" %%a in (input.txt) do set "line=%%a"
set level=0
:nextTag
rem Separate first tag from line
for /F "tokens=1* delims=<" %%a in ("!line!") do (
set "tag=%%a"
set "line=%%b"
)
rem Show first tag in one separate line
if "%tag:~0,1%" equ "/" set /A level-=5
echo !spaces:~0,%level%!^<!tag!
if "%tag:~0,1%" neq "/" set /A level+=5
if defined line goto nextTag
Output:
<xml>
<tag>
<othertag>
</othertag>
</tag>
</xml>
Note that the maximum length of a line that can be processed this way is 8150 characters approx. Also, this solution remove any exclamation-marks from the file. Both points can be fixed, if required.
answered Oct 12 '15 at 12:50
Aacini
50.5k75173
50.5k75173
Great! On the commandline it works, only need to add, that it writes the output in a new file. The character-limitation is no problem, but they exclamation-mark would be a deal, since the files will be used for mocking, so there should be no change.
– user5417542
Oct 12 '15 at 13:15
add a comment |
Great! On the commandline it works, only need to add, that it writes the output in a new file. The character-limitation is no problem, but they exclamation-mark would be a deal, since the files will be used for mocking, so there should be no change.
– user5417542
Oct 12 '15 at 13:15
Great! On the commandline it works, only need to add, that it writes the output in a new file. The character-limitation is no problem, but they exclamation-mark would be a deal, since the files will be used for mocking, so there should be no change.
– user5417542
Oct 12 '15 at 13:15
Great! On the commandline it works, only need to add, that it writes the output in a new file. The character-limitation is no problem, but they exclamation-mark would be a deal, since the files will be used for mocking, so there should be no change.
– user5417542
Oct 12 '15 at 13:15
add a comment |
up vote
0
down vote
try this :
@echo off
call ::beautifyXml c:some.xml
exit /b %errorlevel%
::::::::::::::::::
:beautifyXml
@echo off
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">"%~dp1~"
move /y "%~dp1~" "%~f1" >nul 2>nul
goto :eof
add a comment |
up vote
0
down vote
try this :
@echo off
call ::beautifyXml c:some.xml
exit /b %errorlevel%
::::::::::::::::::
:beautifyXml
@echo off
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">"%~dp1~"
move /y "%~dp1~" "%~f1" >nul 2>nul
goto :eof
add a comment |
up vote
0
down vote
up vote
0
down vote
try this :
@echo off
call ::beautifyXml c:some.xml
exit /b %errorlevel%
::::::::::::::::::
:beautifyXml
@echo off
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">"%~dp1~"
move /y "%~dp1~" "%~f1" >nul 2>nul
goto :eof
try this :
@echo off
call ::beautifyXml c:some.xml
exit /b %errorlevel%
::::::::::::::::::
:beautifyXml
@echo off
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">"%~dp1~"
move /y "%~dp1~" "%~f1" >nul 2>nul
goto :eof
answered Nov 10 at 21:45
npocmaka
41.1k1083126
41.1k1083126
add a comment |
add a comment |
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%2f33077104%2fbatch-script-for-formatting-xml-files-search-for-strings-and-comment-them-out%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
Are you limited to batch as a scripting language ?
– Wald
Oct 12 '15 at 9:17
Unfortunately yes.
– user5417542
Oct 12 '15 at 9:29
2
Use xsl and an identity transform with indenting.
– Kevin Brown
Oct 12 '15 at 9:31
The thing is, I dont need to do this for only one file. I need to do this for many subdirectories. And I have not much knowledge about xsl.
– user5417542
Oct 12 '15 at 9:52