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

后端 未结 6 545
夕颜
夕颜 2020-12-18 09:55

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

相关标签:
6条回答
  • 2020-12-18 10:34

    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.

    0 讨论(0)
  • 2020-12-18 10:39

    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:

    • http://ant.apache.org/manual-1.9.x/index.html
    • http://ant.apache.org/manual-1.9.x/index.html * http://ant.apache.org/manual-1.9.x/Types/fileset.html
    0 讨论(0)
  • 2020-12-18 10:40

    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

    0 讨论(0)
  • 2020-12-18 10:45

    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
    
    0 讨论(0)
  • 2020-12-18 10:50

    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
    
    0 讨论(0)
  • 2020-12-18 11:00
    <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.

    0 讨论(0)
提交回复
热议问题