Results 1 to 10 of 10

Thread: QDomElement's text() for <element> </element> giving me a empty Qstring

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QDomElement's text() for <element> </element> giving me a empty Qstring

    Quote Originally Posted by norobro View Post
    QDomElement::text() will return a QString that will contain 6 spaces if your element contains 6 spaces.
    It doesn't if the node is just whitespace. It will just return an empty Qstring. That's the entire problem and hence, why I'm asking if there's another way to get text from the element using either other classes or doing it manually.

    Edit: Do you mean to say that when I read it using a QXMLSimpleReader, QDomElement::text() will return the whitespace nodes correctly?
    Last edited by CirqueForge; 22nd April 2012 at 15:37.

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QDomElement's text() for <element> </element> giving me a empty Qstring

    Quote Originally Posted by CirqueForge View Post
    Do you mean to say that when I read it using a QXMLSimpleReader, QDomElement::text() will return the whitespace nodes correctly?
    Bingo.

    Modified simple program:
    Qt Code:
    1. #include <QDomDocument>
    2. #include <QXmlSimpleReader>
    3. #include <QDebug>
    4.  
    5. int main(){
    6. QString xmlString("<original> </original>");
    7. doc.setContent(xmlString);
    8. QDomElement docElem = doc.documentElement();
    9. qDebug() << "Without QXmlSimpleReader \t- element contains" <<docElem.text() << "\tlength="<< docElem.text().length();
    10. doc.clear();
    11. source.setData(xmlString);
    12. doc.setContent(&source, &reader);
    13. docElem = doc.documentElement();
    14. qDebug() << "With QXmlSimpleReader \t- element contains" <<docElem.text() << "\tlength="<< docElem.text().length();
    15. }
    16.  
    17. //Output:
    18. //Without QXmlSimpleReader - element contains "" length= 0
    19. //With QXmlSimpleReader - element contains " " length= 6
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Apr 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QDomElement's text() for <element> </element> giving me a empty Qstring

    Quote Originally Posted by norobro View Post
    Bingo.

    Modified simple program:
    Qt Code:
    1. #include <QDomDocument>
    2. #include <QXmlSimpleReader>
    3. #include <QDebug>
    4.  
    5. int main(){
    6. QString xmlString("<original> </original>");
    7. doc.setContent(xmlString);
    8. QDomElement docElem = doc.documentElement();
    9. qDebug() << "Without QXmlSimpleReader \t- element contains" <<docElem.text() << "\tlength="<< docElem.text().length();
    10. doc.clear();
    11. source.setData(xmlString);
    12. doc.setContent(&source, &reader);
    13. docElem = doc.documentElement();
    14. qDebug() << "With QXmlSimpleReader \t- element contains" <<docElem.text() << "\tlength="<< docElem.text().length();
    15. }
    16.  
    17. //Output:
    18. //Without QXmlSimpleReader - element contains "" length= 0
    19. //With QXmlSimpleReader - element contains " " length= 6
    To copy to clipboard, switch view to plain text mode 
    Yeah, I was able to get it to work correctly after doing so. Thank you for your help.

Similar Threads

  1. Get element in Qgridlayout
    By dieter in forum Newbie
    Replies: 1
    Last Post: 12th February 2012, 09:51
  2. how to access ith element of an qstring list?
    By aurora in forum Qt Programming
    Replies: 2
    Last Post: 18th October 2011, 09:37
  3. Replies: 0
    Last Post: 23rd February 2011, 16:36
  4. Remove element of xml
    By VitaliBR in forum Newbie
    Replies: 17
    Last Post: 16th February 2011, 13:28
  5. Help with QML Pathview element
    By chetu1984 in forum Newbie
    Replies: 0
    Last Post: 7th February 2011, 04:49

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.