I have three xml files I'd like to merge.

base.xml
Qt Code:
  1. <symbols>
  2. <circle>
  3. <n_points>0</n_points>
  4. </circle>
  5. </symbols>
To copy to clipboard, switch view to plain text mode 

rect.xml
Qt Code:
  1. <rectangle>
  2. <points>
  3. <n_points>4</n_points>
  4. <point>
  5. <x>1.0</x>
  6. <y>2.0</y>
  7. </point>
  8. <point>
  9. <x>6.0</x>
  10. <y>2.0</y>
  11. </point>
  12. <point>
  13. <x>1.0</x>
  14. <y>4.0</y>
  15. </point>
  16. <point>
  17. <x>6.0</x>
  18. <y>4.0</y>
  19. </point>
  20. </points>
  21. </rectangle>
To copy to clipboard, switch view to plain text mode 

3d.xml
Qt Code:
  1. <rectangle>
  2. <points>
  3. <point>
  4. <z>7.0</z>
  5. </point>
  6. <point>
  7. <z>8.0</z>
  8. </point>
  9. <point>
  10. <z>9.0</z>
  11. </point>
  12. <point>
  13. <z>10.0</z>
  14. </point>
  15. </points>
  16. </rectangle>
To copy to clipboard, switch view to plain text mode 

I want the end result to be like this...
Qt Code:
  1. <symbols>
  2. <circle>
  3. <n_points>0</n_points>
  4. </circle>
  5. <rectangle>
  6. <points>
  7. <n_points>4</n_points>
  8. <point>
  9. <x>1.0</x>
  10. <y>2.0</y>
  11. <z>7.0</z>
  12. </point>
  13. <point>
  14. <x>6.0</x>
  15. <y>2.0</y>
  16. <z>8.0</z>
  17. </point>
  18. <point>
  19. <x>1.0</x>
  20. <y>4.0</y>
  21. <z>9.0</z>
  22. </point>
  23. <point>
  24. <x>6.0</x>
  25. <y>4.0</y>
  26. <z>10.0</z>
  27. </point>
  28. </points>
  29. </rectangle>
  30. </symbols>
To copy to clipboard, switch view to plain text mode 

I constructed the following method:
Qt Code:
  1. void DomHelper::merge(QDomNode mergeInto, const QDomNode &mergeFrom) {
  2. bool foundMatchingNode = false;
  3. QDomNodeList children = mergeInto.childNodes();
  4.  
  5. for (int i = 0; i < children.size(); ++i) {
  6. if (children.at(i) == mergeFrom) {
  7. foundMatchingNode = true;
  8. break;
  9. }
  10. }
  11.  
  12. if (foundMatchingNode) {
  13. DomElementContainer elements(mergeInto, mergeFrom.toElement().tagName());
  14. foreach(QDomElement element, elements) {
  15. QDomNodeList elementChildren = mergeFrom.childNodes();
  16. int count = elementChildren.size();
  17. for (int i = 0; i < count; ++i) {
  18. merge(element, elementChildren.at(i));
  19. }
  20. }
  21. } else {
  22. mergeInto.appendChild(mergeFrom);
  23. m_doc = mergeInto.ownerDocument;
  24. }
To copy to clipboard, switch view to plain text mode 

DomElementContainer is just a class I made instead of using elementsByTagName() ... actually I borrowed it from someone else and modified it but still, it works.

m_doc is what I return to my QTreeView widget to view the result. The result is not what I want though and I've been staring at my code for too long so I can't figure out how to fix it either. I would greatly appreciate some help here.

One other issue is that the merge should be correctly done no matter the order although base.xml always exists before the others. So...

base << rect << 3d

shall produce the same output as....

base << 3d << rect