Combining multiple xml documents into one large one with a batch file

I have a directory of 86 xml files with the same columns and formatting that I need to combine into one large xml file. I am very inexperienced with batch files, and my first attempt was to simply append one file text onto the next using...

FOR %%i IN (directory\*.001) DO type %%i >> directory\combo_file.001 

Unfortunately, this creates a Parse error when i try to open it in excel. I would imagine that this is because many fields and tags are repeated. Does anyone know how I might be able to achieve this? I only need to open this file in excel, so I would be open to converting files to CSV, if that was an option.

Any help is much appreciated, thanks!

1

6 Answers

A very easy approach will be to do a simple copy:

copy *.xml new.xml

The new.xml file created will have all the xml files merged together. You can create a BAT file with the same command

3

Here's a quick batch command that combines all the xml files in the current directory into a single file CombineXML.bat. It wraps all the XML files with a new root node ("<root>").

In your case, however, you may not want to introduce this new layer to the XML. If all you're doing is viewing the XML in a single area (eg: in a web browser) then this works.

--CombineXML.bat-- @echo on rem ==clean up== erase %0.xml rem ==add the root node== echo ^<root^> > %0.txt rem ==add all the xml files== type *.xml >> %0.txt rem ==close the root node== echo ^<^/root^> >> %0.txt rem ==rename to xml== ren %0.txt %0.xml 

The problem is that XML has a single starting tag like: <?xml version="1.0" encoding="UTF-8"?> that would be repeated on the merged document. Also, if there's a root tag that contains all the others, merging you would include the root tag multiple times.

I think that it would be very hard (if possible) to do that in a batch shell, even using a powerful shell and commands like those in Linux/Unix (find, grep, etc).

I would use a simple program (say, VBA) to do that.

edit: I've found that in Excel you can import multiple xml files. You have to go to the Develop tab (show it if it's hidden). Then in the XML group, choose Import and select multiple XML files. That should work.

2

When used with ANT, the <concat> task would be enough:

<echo file="header">&lt;root&gt; </echo> <echo file="footer">&lt;/root&gt; </echo> <concat destfile="concatenated.xml"> <fileset file="header"/> <fileset dir="...."> <include name="**/*.xml"/> </fileset> <fileset file="footer"/> </concat> 

This code produces a common XML element and collects in it the contents of any .xml files it finds according to the file set. See:

  • *
<html xmlns:xi=""> <head> <title>Book Title</title> </head> <body> <xi:include href="chap1.xml"/> <xi:include href="chap2.xml"/> <xi:include href="chap3.xml"/> </body> </html> 

When you process this one file with xslt it looks like all of the files combined.

And I have taken the sample from the above batch command to combine 100+ from one directory into one csv. and works very well.

--CombineXML.bat-- @echo on rem ==clean up== erase %0.xml rem ==add the root node== echo ^<root^> > %0.txt rem ==add all the xml files== type *.xml >> %0.txt rem ==close the root node== echo ^<^/root^> >> %0.txt rem ==rename to csv== ren %0.txt %0.csv 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like