Advice for a script
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.
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...
June 10th, 2004, 12:01 AM
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.
June 10th, 2004, 04:27 AM
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:
This will produce syntactically correct XML, but it may or may not be semantically correct since the ordering of nodes may be significant.
#untested code - caveat emptor
xmlRoot = '''
files = [ 'file1.xml', 'file2.xml', 'file3.xml' ]
xmlTexts = [ open(file, 'r').read() for file in files ]
xml = xmlRoot % ''.join(xmlTexts)
Dave - The Developers' Coach
June 11th, 2004, 12:02 AM
I will utilize your advice and see what I can do.