Page 1 of 1

Findstr to remove whitespace in xml files

Posted: 18 Aug 2016 11:28
by carmine
Hi,

There are multiple xml files in folder d:/global and I want to remove all the whitespace /Tab from each and every line from the xml files in the folder.
I tried "findstr /r /v /c:" " /c:"^$" *.xml "
XML file
<?xml version="1.0" encoding="UTF-8"?>
space...<Document xmlns="urn:iso:std:iso:20022.........">
space....<GrpHdr>
space.....<something>
space......<something>
space........<something>
space....</GrpHdr>
space......<....
space........< ...

After spaces removed
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022.........">
<GrpHdr>
<something>
<something>
<something>
</GrpHdr>
< ....
< ...

Re: Findstr to remove whitespace in xml files

Posted: 18 Aug 2016 11:48
by foxidrive
findstr can't remove characters from a line but there is a batch script called Jrepl that has this as one of it's abilities.

I want to remove all the whitespace /Tab from each and every line

This may not be quite what you want to do though.

Code: Select all

@echo off
for /f "delims=" %%a in ('dir *.xml /b /a-d ') do call jrepl "[ \t]" "" /x /f "%%a" /o -



This uses a native Windows batch script called Jrepl.bat written by Dave Benham
Put it in the same folder, or in a folder that is on the system path.

viewtopic.php?f=3&t=6044

Re: Findstr to remove whitespace in xml files

Posted: 18 Aug 2016 12:27
by aGerman
I hope that jrepl doesn't damage the UTF-8 encoding. There are loads of explanations why you shouldn't apply regular expressions to HTML, XHTML, XML, ...
The funniest "not an explanation" that I ever read :lol:
http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454
Nevermind. It may work anyway ...

Steffen

Re: Findstr to remove whitespace in xml files

Posted: 19 Aug 2016 04:48
by foxidrive
aGerman wrote:I hope that jrepl doesn't damage the UTF-8 encoding. There are loads of explanations why you shouldn't apply regular expressions to HTML, XHTML, XML, ...

That's a point I always overlook.
I've used XML in plain text files here - but I only rarely used xml and so don't have familiarity with it and always treat it as plain text files.

I'm glad you brought that up aGerman and I might remember next time. Or I might not. :oops:
The funniest "not an explanation" that I ever read :lol:

That is funny :)

Re: Findstr to remove whitespace in xml files

Posted: 24 Aug 2016 01:02
by carmine
Hi,

I want to remove all the whitespace /Tab from each and every line

That's working perfectly.

If i want to only remove the leading space before " <"

Thanks.

Re: Findstr to remove whitespace in xml files

Posted: 24 Aug 2016 01:49
by foxidrive
carmine wrote:If i want to only remove the leading space before " <"


In a plain text file this works. It may work with your files too, give it a shot on sample files.

Code: Select all

@echo off
for /f "delims=" %%a in ('dir *.xml /b /a-d ') do call jrepl "^[ \t]*" "" /x /f "%%a" /o -