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 firs
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.
When used with ANT, the <concat> task would be enough:
<echo file="header"><root>
</echo>
<echo file="footer"></root>
</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:
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
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
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
<html xmlns:xi="http://www.w3.org/2001/XInclude">
<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.