Results 1 to 5 of 5

Thread: Need QDomDocument Example

  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 QDomDocument Example

    Hello all experts ,
    Can somebody provide me an example to create a xml file like this?
    I am able to create a single node , but getting problem while inserting other node to it.

    <ui name="UI EDITOR" >
    <ribbon name="ribbon" >
    <tab title="simulation" >
    <panel>
    <button tooltip="scFileOpen('/home/vajindar/Project/auto1.tin')" id="1" />
    <button tooltip="scFileOpen('/home/vajindar/Project/auto2.tin')" id="2" />
    <button tooltip="scFileOpen('/home/vajindar/Project/auto3.tin')" id="3" />
    .
    .
    .
    .
    .
    .
    </panel>
    </tab>
    </ribbon>
    </ui>
    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 QDomDocument Example

    try this
    Qt Code:
    1. #include <QtXml>
    2.  
    3. int main(int argc, char **argv)
    4. {
    5. Q_UNUSED(argc);
    6. Q_UNUSED(argv);
    7.  
    8. QFile file("text.xml");
    9. if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
    10. return 0;
    11.  
    12. QDomElement ui = doc.createElement("ui");
    13. ui.setAttribute("name", "UI EDITOR");
    14.  
    15. QDomElement ribbon = doc.createElement("ribbon");
    16. ribbon.setAttribute("name", "ribbon");
    17. ui.appendChild(ribbon);
    18.  
    19. QDomElement tab = doc.createElement("tab");
    20. tab.setAttribute("title", "simulation");
    21. ribbon.appendChild(tab);
    22.  
    23. QDomElement panel = doc.createElement("panel");
    24. tab.appendChild(panel);
    25.  
    26. for (int i = 0; i < 10; ++i) {
    27. QDomElement button = doc.createElement("button");
    28. button.setAttribute("tooltip", QString("scFileOpen('/home/vajindar/Project/auto%1.tin')").arg(i + 1));
    29. button.setAttribute("id", i + 1);
    30. panel.appendChild(button);
    31. }
    32.  
    33.  
    34. doc.appendChild(ui);
    35. QTextStream out(&file);
    36. out << doc.toString();
    37.  
    38. return 0;
    39. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

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

    ajo (14th March 2013)

  4. #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 QDomDocument Example

    Thank your Sir for your reply ..
    But i don't want to create a file at one time ..
    i want to insert the button node dynamically..
    please give me a Hint
    Thanks & Regards ,

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

  5. #4
    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 QDomDocument Example

    ok, add a method which will create QDomElement for given object
    something like this
    Qt Code:
    1. QDomElement Serializator::toXml(const MyObject &obj, QDomDocument &doc) const
    2. {
    3. QDomElement element = doc.createElement("...");
    4. element.setAttribute(..., ...);
    5. ...
    6. return element;
    7. }
    To copy to clipboard, switch view to plain text mode 

    then
    Qt Code:
    1. QDomDocument Serializator::generateDocument() const
    2. {
    3. QDomElement ui = doc.createElement("ui");
    4. QList<MyObject> objects = this->objects();//method which return a list of objects
    5. foreach(const MyObject &object, objects)
    6. ui.appendChild(toXml(object, doc));
    7.  
    8. doc.appendChild(ui);
    9. return doc;
    10. }
    To copy to clipboard, switch view to plain text mode 

    or even better it's create toXml method in all needed classe,
    of course if you implement them by yourself in this case a code should be like this
    Qt Code:
    1. QDomDocument Serializator::generateDocument() const
    2. {
    3. QDomElement ui = doc.createElement("ui");
    4. QList<MyObject> objects = this->objects();//method which return a list of objects
    5. foreach(const MyObject &object, objects)
    6. ui.appendChild(object.toXml(doc));
    7.  
    8. doc.appendChild(ui);
    9. return doc;
    10. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. #5
    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 QDomDocument Example

    Sir , cann't we do it using insertBefore() and using insertAfter() functions.
    Because i am trying to implement this using these fuctions.

    Sir , could you post the simple example.
    Using insertBefore() and insertAfter();
    Thanks & Regards ,

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

Similar Threads

  1. QDomDocument DTD location
    By Rayven in forum Qt Programming
    Replies: 6
    Last Post: 15th January 2011, 06:30
  2. Split QDomDocument to new QDomDocument
    By estanisgeyer in forum Qt Programming
    Replies: 4
    Last Post: 28th January 2009, 09:59
  3. QDomDocument inside other QDomDocument
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 13th November 2008, 15:27
  4. QDomDocument Speed by 24MB file
    By patrik08 in forum Qt Programming
    Replies: 3
    Last Post: 30th April 2007, 21:35
  5. Adding whitespace in QDomDocument
    By jakamph in forum Newbie
    Replies: 4
    Last Post: 6th February 2006, 22:06

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.