Results 1 to 10 of 10

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

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

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

    Hello. I'm confronted with a problem reading .XML files with my Qt application. When I try and read a XML file that has something like the following with an element with whitespace:

    Qt Code:
    1. ...
    2. <ctrl value="EW45" index="3"/>
    3. <text index="4">
    4. <original> </original>
    5. </text>
    6. ...
    To copy to clipboard, switch view to plain text mode 

    The whitespace between "<original> </original>" isn't preserved and the resulting Qstring is empty. Unfortunately, this creates problems as I have several of these whitespace nodes in the XML files I need to use. I read up on the issue on http://lists.trolltech.com/qt-intere...ad01101-0.html and apprently, it's a really old unfixed "bug" or feature depending on how you look at it. My question is how am I suppose to read the node correctly so I can have these whitespace nodes give me a Qstring that isn't empty?

  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

    From the docs (link):
    Text nodes consisting only of whitespace are stripped and won't appear in the QDomDocument. If this behavior is not desired, one can use the setContent() overload that allows a QXmlReader to be supplied.

  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
    From the docs (link):
    Edit: I'm lacking a bit of sleep on this so what I said previously in this post is kinda off.

    Maybe I'm not getting the full picture with how to use that class to read whitespace, so could you explain it a bit more?

    Right now, my QDomDocument is set as private in my header and predefined. This is the code I use to load the data for retrieving my data.

    Qt Code:
    1. void ScfEncoder::load(QString path)
    2. {
    3. QFile structFile(path);
    4.  
    5. if(!structFile.open(QIODevice::ReadOnly | QIODevice::Text))
    6. {
    7. fatalExit("Failed to open input file!");
    8. }
    9.  
    10. /* Load all data */
    11. xmlData.setContent(structFile.readAll());
    12.  
    13. structFile.close();
    14.  
    15. // Get root
    16. root = xmlData.documentElement();
    17.  
    18. if(root.isNull())
    19. {
    20. fatalExit("Root of the input XML file was not detected!");
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

    I'm just asking if there is any way to retrieve whitespace from a node such as the one I provided above using any existing methods or classes in Qt or do I have to kinda do it manually, if it's even possible?
    Last edited by CirqueForge; 22nd April 2012 at 07: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

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

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

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

  7. #7
    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 16:37.

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

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

  10. #10
    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: 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, 10:51
  2. how to access ith element of an qstring list?
    By aurora in forum Qt Programming
    Replies: 2
    Last Post: 18th October 2011, 10:37
  3. Replies: 0
    Last Post: 23rd February 2011, 17:36
  4. Remove element of xml
    By VitaliBR in forum Newbie
    Replies: 17
    Last Post: 16th February 2011, 14:28
  5. Help with QML Pathview element
    By chetu1984 in forum Newbie
    Replies: 0
    Last Post: 7th February 2011, 05: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.