I want to merge several xml files in perl. Each file is composed of a lot of elements; I need to merge data with the same element from these files. e.g.
file1 has elements {e1, e2, e4}
file2 has elements {e1, e3, e4}
file3 has elements {e2, e4, e5}
so I need to merge e1 of file1 with e1 of file2, merge e2 of file1 and e2 of file3, etc. the merged result will be saved in another file.
Since the size of these files are big, it is not good to merge data file by file, i.e. parse the whole file1, then parse the whole file2 and merge it with file1. etc. because that will consume a lot of memory.
So I plan to merge data element by element. i.e. parse e1 of all files, release memory, then parse element2 of all file, release memory, etc.
Currently I use xml:parser: sax parser to parse and handle the files.
My question is how to implement the merge element by element. I do not know how these files can be controlled to process the same element. Using conditional signal? fork() Or sth. else? Can anybody gives me an example here because I am not familiar with either way. Thx.
here is the example how the data is merged: file1:
<class name="math"
>
<string
>luke1</string
>
<string
>luke2</string
>
</class name
>
<class name="music"
>
<string
>mary1</string
>
<string
>mary2</string
>
</class name
>
file2:
<class name="math"
>
<string
>luke1</string
>
<string
>luke3</string
>
</class name
>
<class name="music"
>
<string
>mary1</string
>
<string
>mary3</string
>
</class name
>
<class name="english"
>
<string
>tom1</string
>
<string
>tom2</string
>
</class name
>
should be merged to another file as:
<class name="math"
>
<string
>luke1</string
>
<string
>luke2</string
>
<string
>luke3</string
>
</class name
>
<class name="music"
>
<string
>mary1</string
>
<string
>mary2</string
>
<string
>mary3</string
>
</class name
>
<class name="english"
>
<string
>tom1</string
>
<string
>tom2</string
>
</class name
>
Note I want to merge element math of all files, then merge element music of all files and then merge element english of all files.