Results 1 to 4 of 4

Thread: creating Xml file using QT

  1. #1
    Join Date
    Jan 2010
    Posts
    28
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default creating Xml file using QT

    hi ,
    i have created an xml file using iostream in Qt , but i come to know we have to use xml Dom for manipulations like save, open,update and delete in the xml file .
    i read the xml dom concept in C++ programming QT , but i didn't understand clearly how to create and read an xml domdocument, i am attaching xml file what i was created , please suggest me how to do this?

    Thankyou
    Attached Files Attached Files
    Last edited by rk0747; 29th January 2010 at 14:37.

  2. #2
    Join Date
    Jan 2010
    Posts
    73
    Thanks
    6
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: creating Xml file using QT

    Reading is easy (this example, I should probably close the file.... oops)
    Qt Code:
    1. QString filename = "C:/test.xml";
    2. QFile file(filename);
    3. if (!file.open(QFile::ReadOnly | QFile::Text))
    4. {
    5. qDebug(qPrintable(QString("Error: Cannot read file %1 %2").arg(filename).arg(file.errorString())));
    6. return;
    7. }
    8. doc.setContent(&file);
    To copy to clipboard, switch view to plain text mode 
    Is your real concern related to processing? Consider this small example that traverses a DOM tree
    Qt Code:
    1. void pp(QTextStream& os, QDomNode node)
    2. {
    3. while (!node.isNull())
    4. {
    5. if (node.isElement())
    6. {
    7. // Ignore attributes for now.
    8. //qDebug(qPrintable(QString("DEBUG: <%1>").arg(node.toElement().tagName())));
    9. os << "<" << node.toElement().tagName() << ">";
    10. if (node.hasChildNodes())
    11. {
    12. QDomNode childNode = node.firstChild();
    13. pp(os, childNode);
    14. }
    15. // Print all children
    16. //qDebug(qPrintable(QString("DEBUG: </%1>").arg(node.toElement().tagName())));
    17. os << "</" << node.toElement().tagName() << ">";
    18. }
    19. else if (node.isText())
    20. {
    21. os << node.toText().data();
    22. }
    23. else
    24. {
    25. qDebug(qPrintable(QString("Unexpected node type:")));
    26. //qDebug(qPrintable(QString("Unexpected node type: %1").arg((int)(node.NodeType()))));
    27. }
    28. node = node.nextSibling();
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 
    The example ignores attributes. The head node that I would pass as an argument is found using doc.documentElement().
    This snippet copies a node out of a DOM tree and removes the node. I then insert the node in a different location.
    Qt Code:
    1. QString xmlOriginal = "<xml><item><a>1</a></item><item><a>2</a></item></xml>";
    2. doc.setContent(xmlOriginal);
    3. QDomElement docElement = doc.documentElement();
    4. QDomNode firstChild = docElement.firstChild();
    5. QDomNode secondChild = firstChild.nextSibling();
    6. QDomNode clonedNode = secondChild.cloneNode(true);
    7. doc.documentElement().removeChild(secondChild);
    8. doc.documentElement().insertBefore(clonedNode, firstChild);
    9. QString s2;
    10. QTextStream os2(&s2);
    11. os2 << "\nStream *****\n" << doc;
    12. qDebug(qPrintable(s2));
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: creating Xml file using QT

    Please dont start another thread for same topic [Original Post ]

  4. #4
    Join Date
    Oct 2009
    Posts
    35
    Thanks
    12
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11

    Default Re: creating Xml file using QT

    Please go through the basics from QtAssistant,all the best

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.