Results 1 to 4 of 4

Thread: Need help merging QDomNodes

  1. #1
    Join Date
    Mar 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Need help merging QDomNodes

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Need help merging QDomNodes

    Quote Originally Posted by Spoon View Post
    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.
    Not from someone else but from me

    The result is not what I want
    So what's wrong with it?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Need help merging QDomNodes

    Quote Originally Posted by wysota View Post
    So what's wrong with it?
    When I merge base >> rect >> 3d everything works out just fine but when I do base >> 3d >> rect, I end up with something that looks like this.

    Qt Code:
    1. <symbols>
    2. <circle>
    3. <n_points>0</n_points>
    4. </circle>
    5. <rectangle>
    6. <points>
    7. <point>
    8. <z>7.0</z>
    9. <x>6.0</x> <!-- should be 1.0 -->
    10. <y>4.0</y> <!-- should be 2.0 -->
    11. </point>
    12. <point>
    13. <z>8.0</z>
    14. <x>2.0</x> <!-- should be 6.0 -->
    15. <y>1.0</y> <!-- should be 2.0 -->
    16. </point>
    17. <point>
    18. <z>9.0</z>
    19. <x>6.0</x> <!-- should be 1.0 --> <!-- y missing completely -->
    20. </point>
    21. <point>
    22. <z>10.0</z>
    23. <y>4.0</y> <!-- can't tell if this is correct or not but x is missing -->
    24. </point>
    25. </points>
    26. <n_points>4</n_points>
    27. </rectangle>
    28. </symbols>
    To copy to clipboard, switch view to plain text mode 

    The order of the tags within the parent node is not relevant but yeah, I screwed up my recursive calls, sadly I still can't see how can fix it.
    Oh and I made a little typo.
    Qt Code:
    1. if (children.at(i) == mergeFrom) {
    To copy to clipboard, switch view to plain text mode 
    should have been ...
    Qt Code:
    1. if (compare(children.at(i), mergeFrom)) {
    To copy to clipboard, switch view to plain text mode 

    where compare is a small routine comparing nodeName() and nodeType() of the two arguments, returning true if both are equal.

    Good job on that element container class btw. I like it

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Need help merging QDomNodes

    It would be easiest for you if you used attributes (like id) for each point to uniquely identify them. Otherwise it is not possible to be sure that you process the right nodes. Someone could remove data from one set but not from the other and everything would go out of sync. Of course it is possible to operate on the order of items (first child, second child, etc.) but I would really advise against it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Merging files (*.001, *.002 etc.)
    By Altertwin in forum Qt Programming
    Replies: 1
    Last Post: 11th July 2010, 11:15
  2. Merging to different QBrushPattern?
    By ashukla in forum Qt Programming
    Replies: 2
    Last Post: 12th January 2008, 04:32
  3. Menubar Merging
    By elcuco in forum Qt Programming
    Replies: 3
    Last Post: 7th April 2007, 15:54
  4. Extracting text from QDomNodes
    By Matt Smith in forum Qt Programming
    Replies: 3
    Last Post: 25th February 2007, 20:27

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.