Results 1 to 2 of 2

Thread: Qt XML and Namespaces

  1. #1
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Qt XML and Namespaces

    This sample code:
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QDebug>
    3. #include <QtXml>
    4.  
    5. int main(int argc, char **argv)
    6. {
    7. QCoreApplication app(argc, argv);
    8.  
    9. const QString uri("urn:test");
    10.  
    11. QDomElement root = doc.createElementNS(uri, "t:root");
    12. doc.appendChild(root);
    13.  
    14. for (int i = 0; i < 3; ++i) {
    15. QDomElement child = doc.createElementNS(uri, "t:child");
    16. // QDomElement child = doc.createElement("t:child"); // Ugly hack works cosmetically
    17. child.appendChild(doc.createTextNode(QString("Child %1").arg(i)));
    18. root.appendChild(child);
    19. }
    20.  
    21. qDebug() << "Generated";
    22. qDebug() << doc.toString(2);
    23.  
    24.  
    25. qDebug() << "\n\nSearch using name space";
    26. QDomNodeList l = doc.elementsByTagNameNS(uri, "child");
    27. for (int i = 0; i < l.size(); ++i) {
    28. QDomElement e = l.at(i).toElement();
    29. qDebug() << e.namespaceURI() << e.localName() << e.tagName();
    30. }
    31.  
    32. return 0;
    33. }
    To copy to clipboard, switch view to plain text mode 

    produces this output:
    Qt Code:
    1. Generated
    2. "<t:root xmlns:t="urn:test">
    3. <t:child xmlns:t="urn:test">Child 0</t:child>
    4. <t:child xmlns:t="urn:test">Child 1</t:child>
    5. <t:child xmlns:t="urn:test">Child 2</t:child>
    6. </t:root>
    7. "
    8.  
    9. Search using name space
    10. "urn:test" "child" "child"
    11. "urn:test" "child" "child"
    12. "urn:test" "child" "child"
    To copy to clipboard, switch view to plain text mode 
    with extraneous XML namespace declarations on every element. What I expected was this XML:
    Qt Code:
    1. <t:root xmlns:t="urn:test">
    2. <t:child>Child 0</t:child>
    3. <t:child>Child 1</t:child>
    4. <t:child>Child 2</t:child>
    5. </t:root>
    To copy to clipboard, switch view to plain text mode 
    which is the sort of thing the MSXML library produces (i.e it handles determining when to declare the namespace and when to simply use it).

    The extra stuff is not too bad with only one name space, but my real application has four intertwined.

    I can get correct looking XML string output if I kludge it and use createElement("t:child") but then, despite appearances, the QDomNode is not in the namespace so the search fails to find these elements in the DOM.

    Is there any way to get QtXml to generate sane markup with namespaces or should I use Xerces-c/libxml2?
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt XML and Namespaces

    or should I use Xerces-c/libxml2?
    I use xerces-c++ exclusively. I figure going with a project whose sole reason for existence is to produce validating XML parsers is a safe bet. It also allows me to write portable code that can be used regardless of whether it is in a Qt app or not. (Sorry if this point of view offends the Qt priesthood and faithful. I'm a pragmatist and don't subscribe to religion).
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Q_DECLARE_INTERFACE and namespaces
    By JPNaude in forum Qt Programming
    Replies: 2
    Last Post: 5th April 2011, 15:39
  2. qtHelp namespaces
    By winkle99 in forum Qt Programming
    Replies: 1
    Last Post: 10th July 2010, 00:32
  3. ui headers and namespaces
    By Rockem in forum Qt Programming
    Replies: 1
    Last Post: 19th August 2009, 18:47
  4. XML Namespaces
    By Rayven in forum Qt Programming
    Replies: 5
    Last Post: 13th September 2008, 09:00
  5. problem with namespaces
    By steve918 in forum General Programming
    Replies: 1
    Last Post: 17th September 2006, 16:55

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.