0

I have large xml file and I need to split into smaller size when the tag is comment and has something like case description. I need to do it in python

I have written this to read the large xml file but I don't know how to split it into a new file

testCases = []
countr = 0
xml_iter = et.iterparse('Main.xml', events=('start', 'end'))
for  event, elem in xml_iter:
    text = elem.text
    
        # if text not in testCases:
        #     testCases.append(text)
    

    if event == 'start':
        print('<%s>' % elem.tag, end = '')
        
        if text != '':
            print(text, end = '')
               

    elif event == 'end':
        print('<%s>' % elem.tag) 
    if text != None and "comment" in elem.tag and "#Test Case ID:" in text:
        break 
Vladislav Povorozniuc
  • 2,149
  • 25
  • 26
sura
  • 1
  • Please share an example xml and your expected output. – Hermann12 Mar 21 '23 at 17:03
  • And where you will keep the spitted file and for what purpose ? There is something called `xslt` that is designed to work with xml. Have a look at https://stackoverflow.com/a/16699042/3057246 – Vinod Srivastav Mar 21 '23 at 21:18

1 Answers1

0

If I understand your question rigth, the easiest solution would probably be something like this:

splitfile = open( name + ".xml", "w" )

name should be a unique string for that part of the original file. "w" says you want to write to the file. If the file doesn't exist, it is created, but if it exist, it will be overwritten, so choose a good namestring (maybe your Test case ID?).

In the open file you now can:

splitfile.write( text )

When you're done with that file make sure to

splitfile.close()

So you can do it something like this:

if text != None and "comment" in elem.tag and "#Test Case ID:" in text:  `
    splitfile = open( str(countr)+ ".xml", "w" )
    countr++
    ...
    splitfile.write(text)
    ...
    splitfile.close()

You can see that my filename is not very good, but you can choose a fitting one.

Your new files will be created in the same directory as your python script.

Sn3nS
  • 36
  • 6