Results 1 to 6 of 6

Thread: QAbstractXmlNodeModel implementation - QDomNodeModel / QXmlQuery

  1. #1
    Join Date
    Aug 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Lightbulb QAbstractXmlNodeModel implementation - QDomNodeModel / QXmlQuery

    I have prepared an implementation (BSD licensed) of QAbstractXmlNodeModel class from QtXmlPatterns module which makes it possible to run QXmlQuery-ies on a QDomDocument. I've called it QDomNodeModel.

    Here's an usage example:
    Qt Code:
    1.  
    2. doc.setContent(QString("<root><elem someAttr='attrValue'><subElem1 /><subElem2 /></elem></root>"), (QString*) 0, (int*) 0, (int*) 0);
    3.  
    4. QXmlQuery q(QXmlQuery::XQuery10);
    5.  
    6. QDomNodeModel m(q.namePool(), doc);
    7.  
    8. q.setFocus(QXmlItem(m.fromDomNode(doc.documentElement())));
    9.  
    10. q.setQuery("elem/@someAttr");
    11.  
    12. QXmlResultItems res;
    13. q.evaluateTo(&res);
    14.  
    15. while (!res.next().isNull())
    16. {
    17. QDomElement elem = m.toDomNode(res.current().toNodeModelIndex()).toElement();
    18. qDebug() << m.toDomNode(res.current().toNodeModelIndex()).toAttr().value();
    19. }
    To copy to clipboard, switch view to plain text mode 

    Download link: http://algoholic.eu/wp-content/uploa...demodel.tar.gz

  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: QAbstractXmlNodeModel implementation - QDomNodeModel / QXmlQuery

    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
    Aug 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QAbstractXmlNodeModel implementation - QDomNodeModel / QXmlQuery

    Well...

    1) It's written in C++, not in Python.
    2) It's not an example for some generic data. It's a working implementation for QDomDocument, which is exactly what I needed (and suppose most people do when they start using QtXmlPatterns along with DOM).


  4. #4
    Join Date
    Mar 2012
    Posts
    14
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QAbstractXmlNodeModel implementation - QDomNodeModel / QXmlQuery

    Hi Stanislaw,

    I would have posted another comment on your blog, but it has gone dead since yesterday, will post there again when live.

    I've discovered the culprit for performance - compareOrder.
    It seems to be very inefficient, profiler screenshot shows hot path.
    Returning any hard-coded DocumentOrder at beginning of function results in almost instantaenous performance vs 5 seconds before parsing a tree with hundreds of nodes.

    Will push my poor c++ skills to the limit trying to optimise, hope you can help.

    Full sized screenshot here: http://www.xemware.com/private/compareOrder.png
    Attached Images Attached Images

  5. #5
    Join Date
    Mar 2012
    Posts
    14
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QAbstractXmlNodeModel implementation - QDomNodeModel / QXmlQuery

    I've been playing around, trying things like caching paths and child indexes and realising that wasn't go to work due to getting copies of nodes and possibly having to redesign the whole implementation to use pointers etc.
    But unless I'm missing something, this works just as well and is QUICK. Results are as expected for initial testing.

    QXmlNodeModelIndex::DocumentOrder QDomNodeModel::compareOrder (
    const QXmlNodeModelIndex & ni1,
    const QXmlNodeModelIndex & ni2 ) const
    {
    QDomNode n1 = toDomNode(ni1);
    QDomNode n2 = toDomNode(ni2);
    if (n1 == n2)
    return QXmlNodeModelIndex::Is;

    int l1 = n1.lineNumber();
    int c1 = n1.columnNumber();
    int l2 = n2.lineNumber();
    int c2 = n2.columnNumber();

    if ( l1 < l2 )
    return QXmlNodeModelIndex::Precedes;

    if ( l1 > l2 )
    return QXmlNodeModelIndex::Follows;

    if ( l1 == l2 && c1 < c2 )
    return QXmlNodeModelIndex::Precedes;
    else
    return QXmlNodeModelIndex::Follows;

  6. #6
    Join Date
    Feb 2009
    Location
    France
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractXmlNodeModel implementation - QDomNodeModel / QXmlQuery

    Thanks a lot for the proposed implementation of compareOrder. I had an occurrence on which the inilial implementation was running for an infinite time (more or less 1 minute). Your implementation is quite immediate with no change in results.

Similar Threads

  1. Problem with QXmlQuery
    By antialias in forum Qt Programming
    Replies: 0
    Last Post: 30th August 2010, 15:16
  2. QXmlQuery not transforming XSL
    By jwc in forum Qt Programming
    Replies: 4
    Last Post: 24th June 2009, 10:26
  3. Help needed with QXmlQuery
    By NoRulez in forum Qt Programming
    Replies: 1
    Last Post: 7th November 2008, 13:25
  4. QXmlQuery / qof - gnucash book
    By tpf80 in forum Qt Programming
    Replies: 0
    Last Post: 29th October 2008, 21:22
  5. Possible to execute QXmlQuery on a QString?
    By chadkeck in forum Qt Programming
    Replies: 3
    Last Post: 8th October 2008, 18:24

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.