#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    74
    Rep Power
    11

    Advice for a script


    Hello,

    Looking for advice as to what would be the best way of making this script. What I have are 10 constant smal xml files
    which would need to be combined together in different combinations. The files are named and not numbered so depending on which ones I need right now they would just get combined and copied together to a separate file.

    Thanks
    Random
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    Is there any sort of criterion by which you are combining them? Or do you not know that until it's time to combine them? The easiest way of concatenating files together depends upon your environment. In a Linux environment, you simply use cat. cat foo bar > fooandbar. Not sure what the best way is to do it in a Windows or Mac environment.

    Of course, if you are doing more than just verbatim concatenation (like you want to combine them but not concatenate), then that gets trickier...
    Debian - because life's too short for worrying.
    Best. (Python.) IRC bot. ever.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    74
    Rep Power
    11
    No there is no criterion by which you are combining them. It's all on the fly type of requests where I am told which are needed and then open a new txt file in Windows and just copy them over one after another.

    Random
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    You cannot just concatenate XML files and expect the result to be valid XML. A valid XML file has one and only one root node, and everything else must be within that root. If you concatenate two or more XML files then you will have more than one root. You also only want the file metadata appearing at the head of the file, not repeated in the middle.

    You could have your files containing snippets of XML, and insert them into the root element, something like this:

    Code:
    #untested code - caveat emptor
    
    xmlRoot = '''
    <?xml version="1.0"?>
    <root>
    %s
    </root>
    '''
    
    files = [ 'file1.xml', 'file2.xml', 'file3.xml' ]
    
    xmlTexts = [ open(file, 'r').read() for file in files ]
    xml = xmlRoot % ''.join(xmlTexts)
    print xml
    This will produce syntactically correct XML, but it may or may not be semantically correct since the ordering of nodes may be significant.

    Dave - The Developers' Coach
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    74
    Rep Power
    11
    Thanks Dave.

    I will utilize your advice and see what I can do.

    Random

IMN logo majestic logo threadwatch logo seochat tools logo