Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
capiot23
- Posts: 5
- Joined: 10 Nov 2023 07:29
#1
Post
by capiot23 » 10 Nov 2023 08:18
Hi everyone,
Can anyone help me write a code to convert xml file from single line to sequential line.
Let say I have this
data.xml (single line)
Code: Select all
<?xml version="1.0" encoding="UTF-8"?><Document xmlns="urn:iso:std:iso:20022:tech:xsd:sese.024.001.01"><SctiesSttlmTxStsAdvc><Id><Id>22052046</Id><CreDtTm><Dt>2023-11-10</Dt></CreDtTm></Id><TxId><AcctOwnrTxId>231110-001-428738</AcctOwnrTxId><CmonId>22052127</CmonId></TxId><MtchgSts><Umtchd><Rsn><Cd><Cd>CMIS</Cd></Cd></Rsn></Umtchd></MtchgSts></SctiesSttlmTxStsAdvc></Document>
I want to convert it as below without
<?xml version...> and
<Document...> at the start and
</Document> at the end.
output.xml (sequential line)
Code: Select all
<SctiesSttlmTxStsAdvc>
<Id>
<Id>22052046</Id>
<CreDtTm>
<Dt>2023-11-10</Dt>
</CreDtTm>
</Id>
<TxId>
<AcctOwnrTxId>231110-001-428738</AcctOwnrTxId>
<CmonId>22052127</CmonId>
</TxId>
<MtchgSts>
<Umtchd>
<Rsn>
<Cd>
<Cd>CMIS</Cd>
</Cd>
</Rsn>
</Umtchd>
</MtchgSts>
</SctiesSttlmTxStsAdvc>
I know this is quite difficult, but I would greatly appreciate it if anyone could help me on this!
Last edited by
capiot23 on 14 Nov 2023 09:41, edited 1 time in total.
-
Aacini
- Expert
- Posts: 1913
- Joined: 06 Dec 2011 22:15
- Location: México City, México
-
Contact:
#3
Post
by Aacini » 11 Nov 2023 12:59
This works:
Code: Select all
@echo off
setlocal EnableDelayedExpansion
for /F "tokens=2* delims=>" %%a in (data.xml) do set "line=%%b"
set "next="
(for /F %%a in (^"!line:^>^<^=^>^
% Do NOT remove this line %
^<!^") do (
if defined next echo !next!
set "next=%%a"
)) > output.xml
Antonio
-
capiot23
- Posts: 5
- Joined: 10 Nov 2023 07:29
#4
Post
by capiot23 » 13 Nov 2023 01:07
Hey thanks, but this doesn't skip the first 2 tags and I don't want it to be format like this
Code: Select all
<xml>
<tag>
<othertag>
</othertag>
</tag>
</xml>
Aacini wrote: ↑11 Nov 2023 12:59
This works:
Code: Select all
@echo off
setlocal EnableDelayedExpansion
for /F "tokens=2* delims=>" %%a in (data.xml) do set "line=%%b"
set "next="
(for /F %%a in (^"!line:^>^<^=^>^
% Do NOT remove this line %
^<!^") do (
if defined next echo !next!
set "next=%%a"
)) > output.xml
Antonio
This works great for short length data as my example input.txt above! Thanks!
But if I tested on my actual data 262kb of file size, it doesn't work, I only get the output as "><=>"
Is it because character limitation? Attached my actual data if you or anyone can help me figure it out.
-
Attachments
-
- input_notwork.xml
- (261.62 KiB) Downloaded 724 times
-
Aacini
- Expert
- Posts: 1913
- Joined: 06 Dec 2011 22:15
- Location: México City, México
-
Contact:
#5
Post
by Aacini » 13 Nov 2023 05:41
The very first post in this site is
How to get help for a batch script - quickly!, so surely you should saw it... Did you read it already? In such a post, there are these phrases:
foxidrive wrote: ↑08 Dec 2014 05:51
Batch files are often specific to your task because your filepaths, text, filenames and data are often used to write code that is both simpler and more efficient.
It makes it easy for people if you provide accurate details. The script you are given is designed to work in your specific situation and they will often add extra code to handle aspects that you may not have thought of...
If you hide your details...
LIKE AN UNUSUAL LARGE FILE SIZE then nobody knows that... In these situations the volunteers can't include appropriate code to handle your special situations because they aren't aware you need it.
By providing poor information about your task the code you are given stands a good chance of failing.
...
Please show respect to those people who give their free time to provide you with code by giving them accurate information about your task.
foxidrive wrote: ↑28 Jul 2016 03:01
What happens in a thread when incorrect or poor details are supplied: is that the volunteers write a script based upon that information and the next post often comes from the question writer saying "It doesn't work!"
Often a very long series of posts begins where we try to ascertain what the problem is and how the code fails and then what the real details of the task are. The script has to be rewritten, and that just wastes the time of those volunteers who are giving you free code and it is terribly frustrating and unsatisfying for volunteers to have to re-write a script for the same task again.
Don't do this.
Processing a data file with a single line larger than 8 KB is a major challenge for a Batch file.
It could be done, but the solution will take some time...
Antonio
-
Aacini
- Expert
- Posts: 1913
- Joined: 06 Dec 2011 22:15
- Location: México City, México
-
Contact:
#6
Post
by Aacini » 13 Nov 2023 07:06
Ok. This new code correctly works with the 262 KB size file. My first attempt fails because I
assumed that no value could have a space!
Code: Select all
@echo off
setlocal EnableDelayedExpansion
set "n=0"
set "last="
call :processLine < data.xml > output.xml
goto :EOF
:processLine
set /P "line="
set "line=!last!!line!"
for /F "delims=" %%a in (^"!line:^>^<^=^>^
% Do NOT remove this line %
^<!^") do (
set /A n+=1
if !n! gtr 3 echo !last!
set "last=%%a"
)
set "n=2"
if "!last!" neq "</Document>" goto processLine
exit /B
The program generated an output.xml file with 21668 lines...
Antonio
-
capiot23
- Posts: 5
- Joined: 10 Nov 2023 07:29
#7
Post
by capiot23 » 14 Nov 2023 09:01
Sir, apologize for my overlooked on the rules. I'm just worry I'd leak out some private information from the actual data, but after read by lines, there's nothing much to worry.
Aacini wrote: ↑13 Nov 2023 07:06
Ok. This new code correctly works with the 262 KB size file. My first attempt fails because I
assumed that no value could have a space!
Code: Select all
@echo off
setlocal EnableDelayedExpansion
set "n=0"
set "last="
call :processLine < data.xml > output.xml
goto :EOF
:processLine
set /P "line="
set "line=!last!!line!"
for /F "delims=" %%a in (^"!line:^>^<^=^>^
% Do NOT remove this line %
^<!^") do (
set /A n+=1
if !n! gtr 3 echo !last!
set "last=%%a"
)
set "n=2"
if "!last!" neq "</Document>" goto processLine
exit /B
The program generated an output.xml file with 21668 lines...
Antonio
Bravo!!! This works perfectly as I wanted even for larger file size.
Thank you so much for your time, sir. I've put your name in my batch file credit to you for coding this.
I'll learn from the code although it's short, but no doubt it's kind of unreadable for my current level
-
Squashman
- Expert
- Posts: 4486
- Joined: 23 Dec 2011 13:59
#8
Post
by Squashman » 16 Nov 2023 11:38
capiot23 wrote: ↑13 Nov 2023 01:07
Hey thanks, but this doesn't skip the first 2 tags and I don't want it to be format like this
Code: Select all
<xml>
<tag>
<othertag>
</othertag>
</tag>
</xml>
Wasn't meant to be the exact code. Since you provided no attempt at coding a solution, at the very least I gave you a starting point to adapt the code.