Results 1 to 4 of 4

Thread: Extracting text from QDomNodes

  1. #1
    Join Date
    Jan 2006
    Location
    New Malden (near London)
    Posts
    32
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11

    Default Extracting text from QDomNodes

    Hi everyone,

    I'm trying to set up a DOM-based parser to extract blog post templates for an application I'm writing. The XML format I'm using goes like this:

    Qt Code:
    1. <?xml version="1.0"?>
    2. <QuickpostTemplates>
    3. <Template>
    4. <title>Title to be displayed in menu</title>
    5. <templateString>The actual template</templateString>
    6. </Template>
    7. [and as many more templates as the user requires]
    8. </QuickpostTemplates>
    To copy to clipboard, switch view to plain text mode 

    This is the function I've been using to extract the title and template strings from the XML:

    Qt Code:
    1. QString templateFile;
    2. QString errorString;
    3. QString currentTitle, currentTemplate;
    4. int errorLine, errorCol;
    5. QDomNodeList titles, templateStrings;
    6. int numTemplates, t;
    7. QTextStream ts( stdout );
    8.  
    9. // Define templateFile; omitted
    10.  
    11. if( QFile::exists( templateFile ) ) {
    12. QDomDocument domDoc( "quickpostTemplates" );
    13. QFile file( templateFile );
    14. if( !domDoc.setContent( &file, true, &errorString, &errorLine, &errorCol ) )
    15. [Warning message box]
    16. else {
    17. titles = domDoc.elementsByTagName( "title" );
    18. templateStrings = domDoc.elementsByTagName( "templateString" );
    19.  
    20. if( titles.size() ) {
    21. quickpostTemplateActions.clear();
    22. numTemplates = ( titles.size() >= templateStrings.size() ?
    23. titles.size() : templateStrings.size() );
    24. qDebug( "Populating template menu" );
    25. for( t = 0; t < numTemplates; t++ ) {
    26. currentTitle = titles.item( t ).nodeValue();
    27. currentTemplate = templateStrings.item( t ).nodeValue();
    28. ts << titles.item( t ).nodeValue();
    29. ts << templateStrings.item( t ).nodeValue();
    30. quickpostTemplateActions.append( new QuickpostTemplate( t, currentTitle, currentTemplate, 0 ) );
    31. templateMenu->addAction( quickpostTemplateActions[t] );
    32. }
    33. }
    34. else
    35. qDebug( "No templates found." );
    36. }
    37. }
    To copy to clipboard, switch view to plain text mode 

    The titles and template strings are stored in two separate QLists of QDomNodes, and the loop steps through both to extract the strings from each list in turn. The problem is that there are two methods for extracting the text from the nodes: either nodeName.nodeValue() or nodeName.toText().data(), and neither or them seem to work for me. They both keep extracting blank text, and the result is that the templateMenu (QMenu object) is populated by actions identified by blank strings.

    If I replaced the lines beginning with "ts <<" with the following:

    Qt Code:
    1. ts << titles.item( t );
    2. ts << templateStrings.item( t );
    To copy to clipboard, switch view to plain text mode 

    then the entire element, opening and closing tags and all, are sent to standard output.

    Can anyone see what I'm doing wrong?

  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: Extracting text from QDomNodes

    How about:
    Qt Code:
    1. QDomDocument doc = ...
    2. QDomElement root = doc.documentElement();
    3. for(QDomElement elem = root.firstChildElement("Template");
    4. !elem.isNull();
    5. elem = elem.nextSiblingElement("Template")){
    6. qDebug("TITLE: %s", qPrintable(elem.firstChildElement("Title").text());
    7. qDebug("CONTENT: %s", qPrintable(elem.firstChildElement("templateString").text()));
    8. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Extracting text from QDomNodes

    The docs say:
    QString QDomNode::nodeValue () const
    Returns the value of the node.
    The meaning of the value depends on the subclass:
    (...)
    All the other subclasses do not have a node value and will return an empty string.
    Unfortunately QDomElement falls into "other subclasses" category.

    Try:
    Qt Code:
    1. titles.item( t ).toElement().text();
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to jacek for this useful post:

    Matt Smith (25th February 2007)

  5. #4
    Join Date
    Jan 2006
    Location
    New Malden (near London)
    Posts
    32
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Extracting text from QDomNodes

    Thanks an awful lot, I am not all that familiar with DOM and did not think of that. It works now.

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Problem pasting text into a QTextEdit
    By Spockmeat in forum Qt Programming
    Replies: 8
    Last Post: 14th March 2009, 14:36
  3. Editable text in QGraphicsView
    By wysota in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2007, 15:30
  4. visible text of textedit
    By regix in forum Qt Programming
    Replies: 3
    Last Post: 26th June 2006, 09:02
  5. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 06:03

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.