I have like 50 XML files - what is the easiest way to append them all?

Discussion in 'Web Design and Development' started by spaceballl, Feb 14, 2008.

  1. spaceballl macrumors 68030

    spaceballl

    Joined:
    Nov 2, 2003
    Location:
    San Francisco, CA
    #1
    So i have like 50 xml files, each w/ a root node, and i just want one file, w/ all the nodes - what's the best way to do this? Thanks!
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    Could use some more info. Do the files have a common root node, which will remain to be the root node? Do you have some example XML of what these files look like?

    You might be able to use something that appends the files (all text) then perhaps use regular expression search and replace to remove the unneeded parts. If this is something tht gets repeated a lot, this won't be a very good option.
     
  3. spaceballl thread starter macrumors 68030

    spaceballl

    Joined:
    Nov 2, 2003
    Location:
    San Francisco, CA
    #3
    No common root node.

    Look at it this way...

    file1.xml:
    <Record1>random stuff here</Record1>​
    file2.xml:
    <Record2>random stuff here</Record2>​
    file3.xml:
    <Record3>random stuff here</Record3>​

    What I want is...
    onefile.xml:
    <Record1>random stuff here</Record1>
    <Record2>random stuff here</Record2>
    <Record3>random stuff here</Record3>


    I appreciate the help!
     
  4. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #4
    Well you can append files together pretty easy with Terminal.

    cat *.xml > Master.xml

    This will take all XML files in the current directory and catenate them together into a single file. Then to get rid of the XML declarations you can use,

    grep -v "^<?xml" Master.xml > Master2.xml

    You'll have to put the top XML declaration back in though.
     
  5. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #5
    Yeah, I'd recommend the cat command too. If you're not comfortable with grep you could always just open the whole thing up in a text editor afterwards and manually fix up whatever's left.
     

Share This Page