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
    Here's a simple example that hopefully will help :
    Qt Code:
    1. #include <QDomDocument>
    2. #include <QXmlSimpleReader>
    3. #include <QDebug>
    4.  
    5. int main(){
    6.  
    7. QString xmlString("<original> </original>");
    8. source.setData(xmlString);
    9. doc.setContent(&source, &reader);
    10. QDomElement docElem = doc.documentElement();
    11. qDebug() << docElem.tagName() << docElem.text();
    12. }
    To copy to clipboard, switch view to plain text mode 
    Outputs: "original" " "
    Kinda helpful for me to understand what QXmlSimpleReader does but I think you are misunderstanding me.

    The thing is from "<original> </original>", I want to get a Qstring with " ". As in, I want to read the whitespace and nothing else. I want it so that not only when I parse "<original> </original>" do I get a whitespace but for some control tags, "<control>  </control>", the Qstring would give me " ". Is there a way from any classes in Qt or any kind of manual implementation I can use to read a node so it will do that for me? Or is there some way I can bypass this non-whitespace reading?

    Edit: Even the forum doesn't seem to respect whitespace handling, haha... I meant for <control></control> to have 6 spaces in between.

  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
    As in, I want to read the whitespace and nothing else.
    QDomElement::text() will return a QString that will contain 6 spaces if your element contains 6 spaces.

  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
    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.

  4. #4
    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 

  5. #5
    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: QDomElement's text() for <element> </element> giving me a empty Qstring

    Quote Originally Posted by CirqueForge View Post
    Edit: Even the forum doesn't seem to respect whitespace handling, haha... I meant for <control></control> to have 6 spaces in between.
    It's not the forum, it's your web browser.
    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.


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.