Results 1 to 7 of 7

Thread: Need help for QDomDocument

  1. #1
    Join Date
    Dec 2008
    Location
    PUNE (INDIA)
    Posts
    49
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Need help for QDomDocument

    Qt Code:
    1. void Ribbon::WriteXml(int id ,QString location)
    2. {
    3. QFile file("xml/node.xml");
    4. QDomDocument document ;
    5.  
    6. if(id==1) //creating ui , ribbon , tab , pabel , button node for the first time
    7. {
    8. QDomElement uiElementNode , documentElement , ribbonElementNode , tabElementNode , panelElementNode , buttonElementNode;
    9. if(!file.open(QIODevice::ReadWrite | QIODevice::Text))
    10. {
    11. qDebug("Error... in opening a file");
    12. return ;
    13. }
    14.  
    15. uiElementNode = document.createElement("ui");
    16. uiElementNode.setAttribute("name", "UI EDITOR");
    17. document.appendChild(uiElementNode);
    18.  
    19. ribbonElementNode = document.createElement("ribbon");
    20. uiElementNode.appendChild(ribbonElementNode);
    21.  
    22. tabElementNode = document.createElement("tab");
    23. ribbonElementNode.appendChild(tabElementNode);
    24.  
    25. panelElementNode = document.createElement("panel");
    26. tabElementNode.appendChild(panelElementNode);
    27.  
    28. buttonElementNode = document.createElement("button");
    29. panelElementNode.appendChild(buttonElementNode);
    30.  
    31. }
    32. else // here we need to insert button node to the panel
    33. {
    34. if(!file.open(QIODevice::ReadWrite | QIODevice::Text))
    35. {
    36. QMessageBox::warning(this, tr("Error"), "Error... in opening a file");
    37. return ;
    38. }
    39.  
    40. if( !document.setContent( &file ) )
    41. {
    42. QMessageBox::warning(this, tr("Error"), "Failed to parse the file into a DOM tree.");
    43. return ;
    44. }
    45.  
    46. QDomElement documentElement = document.documentElement(); //returns the root element
    47. QDomNodeList elements = documentElement.elementsByTagName( "panel" ); //searching for the panel node
    48. QDomElement panel = elements.at(0).toElement();
    49. QDomElement newButton = document.createElement("button");//creating new button
    50. panel.appendChild(newButton); //appending to the panel node
    51.  
    52. }
    53. QTextStream out(&file);
    54. out << document.toString(); //writting to a file
    55. file.close();
    56. }
    To copy to clipboard, switch view to plain text mode 

    Hello All experts this is my code to create the xml file.

    I want result in following format
    xml Code:
    1. <ui name="UI EDITOR" >
    2. <ribbon>
    3. <tab/>
    4. <panel>
    5. <button/>
    6. <button/>
    7. </panel>
    8. </ribbon>
    9. </ui>
    To copy to clipboard, switch view to plain text mode 
    But i am getting result in this format
    xml Code:
    1. <ui name="UI EDITOR" >
    2. <ribbon>
    3. <tab/>
    4. <panel>
    5. <button/>
    6. </panel>
    7. </ribbon>
    8. </ui>
    9. <ui name="UI EDITOR" >
    10. <ribbon>
    11. <tab/>
    12. <panel>
    13. <button/>
    14. <button/>
    15. </panel>
    16. </ribbon>
    17. </ui>
    To copy to clipboard, switch view to plain text mode 

    Please tell me where i am commiting mistake

    I WANT TO INSERT THE button NODE DYNAMICALLY
    Last edited by jpn; 22nd April 2009 at 11:43. Reason: sigh
    Thanks & Regards ,

    Vajindar Laddad .
    Trainee Developer.
    (INDIA).
    91+9325014248

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Need help for QDomDocument

    why you can't just append a child?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Dec 2008
    Location
    PUNE (INDIA)
    Posts
    49
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Need help for QDomDocument

    panel.appendChild(newButton); //appending to the panel node

    Sir , i am appending a child .. but not getting the desired output, repetation of ui , ribbon tab...etc does occure
    Thanks & Regards ,

    Vajindar Laddad .
    Trainee Developer.
    (INDIA).
    91+9325014248

  4. #4
    Join Date
    Dec 2008
    Location
    PUNE (INDIA)
    Posts
    49
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Need help for QDomDocument

    In what other way i can do the same
    Last edited by vajindarladdad; 22nd April 2009 at 08:47. Reason: updated contents
    Thanks & Regards ,

    Vajindar Laddad .
    Trainee Developer.
    (INDIA).
    91+9325014248

  5. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Need help for QDomDocument

    as I understand you need to add data between these tags
    <ui name="UI EDITOR" >
    <ribbon>
    ....
    </ribbon>
    <ui name="UI EDITOR" >
    right?

    why you can't create a method wich will generate whole xml at all?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. The following user says thank you to spirit for this useful post:

    vajindarladdad (22nd April 2009)

  7. #6
    Join Date
    Dec 2008
    Location
    PUNE (INDIA)
    Posts
    49
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Need help for QDomDocument

    Sir if you could provide me an example code to do so ..
    I would be very grateful.
    Thanks & Regards ,

    Vajindar Laddad .
    Trainee Developer.
    (INDIA).
    91+9325014248

  8. #7
    Join Date
    Dec 2008
    Location
    PUNE (INDIA)
    Posts
    49
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Need help for QDomDocument

    Thanks ... here is a code to do so......

    [CODE ]
    void Ribbon::WriteXml(int id ,QString location)
    {
    QFile file("xml/node.xml");
    QDomDocument document ;

    if(id==1)
    {
    QDomElement uiElementNode , documentElement , ribbonElementNode , tabElementNode , panelElementNode , buttonElementNode;
    if(!file.open(QIODevice::ReadWrite | QIODevice::Text))
    {
    qDebug("Error... in opening a file");
    return ;
    }

    uiElementNode = document.createElement("ui");
    uiElementNode.setAttribute("name", "UI EDITOR");
    document.appendChild(uiElementNode);

    ribbonElementNode = document.createElement("ribbon");
    uiElementNode.appendChild(ribbonElementNode);

    tabElementNode = document.createElement("tab");
    ribbonElementNode.appendChild(tabElementNode);

    panelElementNode = document.createElement("panel");
    tabElementNode.appendChild(panelElementNode);

    buttonElementNode = document.createElement("button");
    buttonElementNode.setAttribute("id",QString::number(id));
    buttonElementNode.setAttribute("tooltip",location);
    panelElementNode.appendChild(buttonElementNode);

    QTextStream out(&file);
    out << document.toString(); //writting to a file
    file.close();

    }
    else // here we need to insert button node to the panel
    {
    if(!file.open(QIODevice::ReadWrite | QIODevice::Text))
    {
    QMessageBox::warning(this, tr("Error"), "Error... in opening a file");
    return ;
    }

    if( !document.setContent( &file ) )
    {
    QMessageBox::warning(this, tr("Error"), "Failed to parse the file into a DOM tree.");
    return ;
    }
    QDomElement documentElement = document.documentElement(); //returns the root element
    QDomNodeList elements = documentElement.elementsByTagName( "panel" ); //searching for the panel node
    QDomElement panel = elements.at(0).toElement();
    QDomElement newButton = document.createElement("button");//creating new button
    newButton.setAttribute("id",QString::number(id));
    newButton.setAttribute("tooltip",location);
    panel.appendChild(newButton); //appending to the panel node
    file.close();

    if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) //opening a file again to write the new content
    {
    QMessageBox::warning(this, tr("Error"), "Error... in opening a file");
    return ;
    }
    QTextStream out(&file);
    out << document.toString(); //writting to a file
    file.close();
    }
    }

    [ /CODE ]
    Thanks & Regards ,

    Vajindar Laddad .
    Trainee Developer.
    (INDIA).
    91+9325014248

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.