Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Merging xml files

502 views
Skip to first unread message

Shimpa

unread,
May 1, 2002, 8:59:01 PM5/1/02
to
Hi,

I have n *.xml files with the same format as below:
<a>
<b>
</b>
<c>
</c>
</a>

Please let me know how can I merge them into one single file of the
same format as using a batch script.

<a>
<b>
</b>
<b>
</b>
<b>
</b>
<c>
</c>
<c>
</c>
</a>

Herbert Kleebauer

unread,
May 2, 2002, 4:09:15 PM5/2/02
to
Shimpa wrote:
>
> Hi,
>
> I have n *.xml files with the same format as below:
> <a>
> <b>
> </b>
> <c>
> </c>
> </a>
>
> Please let me know how can I merge them into one single file of the
> same format as using a batch script.

First file:

<a>
<b>
text1b
</b>
<c>
text1c
</c>
</a>

Second file:

<a>
<b>
text2b
</b>
<c>
text2c
</c>
</a>

Result file:

<a><b>
text1b
</b><b>
text2b
</b><c>
text1c
</c><c>
text2c
</c></a>


@echo off
echo XP,;P_,EP0Ex0Ez0Em3Em-ZBP(Eiu![j@_YQ2M@53#@CI~.8'uxCISZ[SC5.bM!C>ech.com

echo Bj@jzh`0X-`/PPPPPPa(DE(DM(DO(Dh(Ls(Lu(LX(LeZRR]EEEUYRX2Dx=>sbs2.com
echo 0DxFP,0Xx.t0P,=XtGsB4o@$?PIyU!WvX0GwUY Wv;ovBX2Gv0ExGIuht6>>sbs2.com
echo ?@}IKuNWpe~Fpe?FNHlF?wGMECIQqo{Ox{T?kPv@jeoSeIlRFD@{AyEKj@>>sbs2.com
echo iqe~1NeAyR?mHAG~BGRgB{~H?o~TsdgCYqe?HR~upkpBG?~slJBCyA?@xA>>sbs2.com
echo LZp{xq`Cs?H[C_vHDyB?Hos@QslFA@wQ~~x}viH}`LYNBGyA?@xAB?sUq`>>sbs2.com
echo LRy@PwtCYQEuFK@A~BxPtDss@fFqjVmzD@qBEOEenU?`eHHeBCMs?FExep>>sbs2.com
echo LHsPBGyA?@xAunjzA}EKNs@CA?wQpQpKLBHv?s`WJ`LRCYyIWMJaejCksl>>sbs2.com
echo H[GyFGhHBwHZjjHeoFasuFUJeHeB?OsQH[xeHCPvqFj@oq@eNc?~}Nu??O>>sbs2.com
echo ~oEwoAjBKs?Zp`LBzHQzyEFrAWAG{EFrAqAGYwHTECIQ{coKIsaCsf{Oe~>>sbs2.com
echo CK}Ayre~CNFA{rAyEKFACrA{EKGAjbA}eKGSjNMtQFtc{OAyDGFj?{FDGQ>>sbs2.com
echo KAjNVk_OCAx@e?f{o?CosI}1EGizhljJ~H1ZeG}JBA~rACBMDGjjDG@g0>>sbs2.com

ech "<a>">_._
for %%i in (*.xml) do (
type %%i|sbs2 1 "<b>' '<b>"|sbs2 1 "</b>" "</b>'">>_._)
for %%i in (*.xml) do (
type %%i|sbs2 1 "<c>' '<c>"|sbs2 1 "</c>" "</c>'">>_._)
ech "</a>">>_._
del ech.com
del sbs2.com

:: Usage: sbs2.com NUMBER "STRING1" "STRING2" <infile >outfile
::
:: infile and outfile must not be the same file!
::
:: Substitutes the NUMBER ocurrence of STRING1 in infile by
:: STRING2 and writes the result to outfile
::
:: You can include any character in STRING1/2 by using its
:: hex value (e.g. $0d for <CR> or $1a for EOF)
::
:: If NUMBER = 0 all STRING1 are substituted by STRING2
::
:: If an error is detected or nothing is substituted, erorrlevel=0
:: replace the original file only if errorlevel>=1 (=number of
:: substitutions).
::
:: Instead of the double quotes (") you can also use single quotes (')
:: with a different meaning for string2:
:: "string2" : normal substitution
:: 'string2" : before substitution the output file is rewinded
:: "string2' : after substitution the outputfile is closed

Ritchie Lawrence

unread,
May 2, 2002, 5:26:54 PM5/2/02
to
=================================== start mergexml.cmd ===========================
@echo off&setlocal

set FILESPEC=*.xml
set TEMPFILE=xml.tmp

:: Init header & footer, '<' and '>' characters must be escaped with '^'
set H=^<a^>
set F=^</a^>

:: Init temp file
type nul >%TEMPFILE%

:: Output content to temp file
for /f "tokens=1* delims=:" %%a in (
'findstr /v "%H% %F% </" %FILESPEC%'
) do echo %%b >> xml.tmp

:: Output header
for /f "tokens=*" %%a in ("%H%") do echo %%a

:: Sort the temp file and add closing tag
for /f "tokens=1* delims=<" %%a in ('type xml.tmp ^| sort') do (
echo ^<%%b
echo ^</%%b
)

:: Output footer
for /f "tokens=*" %%a in ("%F%") do echo %%a
==================================== end mergexml.cmd ==============================

The above _may_ be what you're looking for. Assume the following two files
are in the current directory:-

======= start 1.xml and 2.xml ============


<a>
<b>
</b>
<c>
</c>
</a>

======= end 1.xml and 2.xml ==============

Then the following command:-

mergexml > merged.xml

creates a file called merged.xml containing:-

<a>
<b>
</b>
<b>
</b>

<c>
</c>
<c>
</c>
</a>

PS I'm sure this can be done without the temp file... anyone?

--
Ritchie
Undo address for email.

"Shimpa" <shimpa...@rediffmail.com> wrote in message news:37c7ab7d.02050...@posting.google.com...

Shimpa

unread,
May 3, 2002, 3:28:00 PM5/3/02
to
Hi Ritchie,

I tried this , thanks a lot...
a few issues:

In the files say for file 1 and 2
<aa>
<bb>
text1
</bb>
<cc>
text2
</cc>
</aa>

the merging should happen for the text1 and text2 fields too:

<aa>
<bb>
text1
</bb>
<bb>
text1
</bb>
<cc>
text2
</cc>
<cc>
text2
</cc>
</aa>


Thanks

"Ritchie Lawrence" <rlaw...@commanddoline.co.uk> wrote in message news:<3cd1b...@mk-nntp-1.news.uk.worldonline.com>...

Shimpa

unread,
May 3, 2002, 9:28:55 PM5/3/02
to
Hi Herbert,


I cannot understand the comments below. :(
Also, I tried running the batch file below...
I still get the <a> </a> tag whenever I merge multiple files.

Thanks
Shimpa


Herbert Kleebauer <kl...@unibwm.de> wrote in message news:<3CD19CEB...@unibwm.de>...

Ritchie Lawrence

unread,
May 4, 2002, 9:08:16 AM5/4/02
to
Hi Shimpa,
Because you omitted the data in your earlier examples, I figured you must have managed
to append the data immediately after the opening tags, hence no need to include it...
Anyway, here's version 2 (no temp file this time).

As with any NT batch file that reads a file containing posion characters, certain
combinations of double quotes will break it. Pls prove me wrong... anyone?

By the way, couldn't this be accomplished by querying some kind of webserver/databse?

======================== start mergexml.cmd =================================
@echo off&setlocal

set FILESPEC=*.xml

:: Init header, '<' and '>' characters must be escaped with '^'
set H=^<aa^>
set F=^</aa^>

:: Load opening tags and data into memory
set i=999999999


for /f "tokens=1* delims=:" %%a in (

'findstr /v "\<%H% </" %FILESPEC%'
) do set DATA="%%b"&call:concat

:: Output header
for /f "tokens=*" %%a in ("%H%") do echo %%a

:: Output content
for /f "tokens=1* delims==" %%a in ('set XMLRECORD1 ^| sort /+20') do (
for /f "tokens=1* delims=^<^>" %%c in (%%b) do (
echo ^<%%c^>
echo %%d
echo ^</%%c^>
)
)

:: Output footer
for /f "tokens=*" %%a in ("%F%") do echo %%a

goto:eof

:concat (Concatenates opening tag and data)
:: Fix poison characters before removing double quotes
set DATA=%DATA:<=^<%
set DATA=%DATA:>=^>%
for /f "tokens=*" %%a in (%DATA%) do set DATA=%%a

set/a i+=1,FLAG=i%%2

if %FLAG% EQU 0 (
set TAG=%DATA%
) else (
call set XMLRECORD%i%="%TAG%%DATA%"
)
========================== end mergexml.cmd ===============================

PS Note, I changed the header and footer to <aa> and </aa> to reflect your
most recent example.

--
Ritchie
Undo my address for email.

Herbert Kleebauer

unread,
May 4, 2002, 9:36:24 AM5/4/02
to
Shimpa wrote:

> I cannot understand the comments below. :(

It is just the description of the included, very simple substitution
tool.

> Also, I tried running the batch file below...
> I still get the <a> </a> tag whenever I merge multiple files.

Can you post (a link to) some example files, so I can test it?

Herbert Kleebauer

unread,
May 6, 2002, 10:27:31 AM5/6/02
to
Shimpa wrote:
>
> Hi Herbert,
>
> I cannot understand the comments below. :(
> Also, I tried running the batch file below...
> I still get the <a> </a> tag whenever I merge multiple files.

use instead of:

> > ech "<a>">_._
> > for %%i in (*.xml) do (
> > type %%i|sbs2 1 "<b>' '<b>"|sbs2 1 "</b>" "</b>'">>_._)
> > for %%i in (*.xml) do (
> > type %%i|sbs2 1 "<c>' '<c>"|sbs2 1 "</c>" "</c>'">>_._)
> > ech "</a>">>_._
> > del ech.com
> > del sbs2.com

the following:

ech "<a>">_._
for %%i in (*.xml) do (

type %%i|sbs2 1 "<b>' '<b>" >_.1
type _.1|sbs2 1 "</b>" "</b>'">>_._)


for %%i in (*.xml) do (

type %%i|sbs2 1 "<c>' '<c>" >_.1
type _.1|sbs2 1 "</c>" "</c>'">>_._)


ech "</a>">>_._
del ech.com
del sbs2.com

del _.1

0 new messages