Skip to content
Advertisement

Merge xml files with nested elements without external libraries

I am trying to merge multiple XML files together using Python and no external libraries. The XML files have nested elements.

Sample File 1:

JavaScript

Sample File 2:

JavaScript

What I Want:

JavaScript

What I have tried:

From this answer.

JavaScript

What I Get:

JavaScript

I hope you can see and understand my problem. I am looking for a proper solution, any guidance would be wonderful.

To clarify the problem, using the current solution that I have, nested elements are not merged.

Advertisement

Answer

What the code you posted is doing is combining all the elements regardless of whether or not an element with the same tag already exists. So you need to iterate over the elements and manually check and combine them the way you see fit, because it is not a standard way of handling XML files. I can’t explain it better than code, so here it is, more or less commented:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement