Results 1 to 9 of 9

Thread: QDomDocument Eats Up My Spaces

  1. #1

    Exclamation QDomDocument Eats Up My Spaces

    I'm using QDomDocument to create XML strings.

    Expected result:
    Qt Code:
    1. <container>
    2. <datavalue> </datavalue>
    3. </container>
    To copy to clipboard, switch view to plain text mode 

    Actual outcome:
    Qt Code:
    1. <container>
    2. <datavalue></datavalue>
    3. </container>
    To copy to clipboard, switch view to plain text mode 

    But if my datavalue is like " 1 ", then it works. It's only happening when there are no characters at all.

    Do you guys know any easy work around for it?

  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: QDomDocument Eats Up My Spaces

    Whitespaces are transparent to xml, these are all equivalent:
    xml Code:
    1. <a> <b> </b> </a>
    2. <a><b/></a>
    3. <a>
    4. <b>
    5. <!-- multiple spaces here -->
    6. </b>
    7. </a>
    To copy to clipboard, switch view to plain text mode 
    If you want to preserve whitespaces you might try placing them in a CDATA section.
    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

    Default Re: QDomDocument Eats Up My Spaces

    That's not really an option for me, since I need follow a format.

    But, the easiest way is to first replace all the spaces with character X and then replace back after the QDomDocument is done and I'm getting it to a string?

  4. #4
    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: QDomDocument Eats Up My Spaces

    Quote Originally Posted by ManuMies View Post
    That's not really an option for me, since I need follow a format.
    Then you are not really using xml and QDomDocument might not be for you.

    But, the easiest way is to first replace all the spaces with character X and then replace back after the QDomDocument is done and I'm getting it to a string?
    That's probably a bad idea, you'll get a lot of X in places you won't be able to predict and won't be able to determine whether there was originally a space there or maybe a 'X' character.
    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.


  5. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QDomDocument Eats Up My Spaces

    An XML parser should always present the data unchanged to the user, including white spaces.

    http://www.w3.org/TR/2008/REC-xml-20...ec-white-space
    Last edited by tbscope; 4th November 2010 at 14:49.

  6. #6
    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: QDomDocument Eats Up My Spaces

    Quote Originally Posted by tbscope View Post
    An XML parser should always present the data unchanged to the user, including white spaces.
    Only if explicitly declared that white-spaces are to be preserved. In my opinion if you want an explicit whitespace, it's best to encode it as an entity, e.g. as &nbsp; or &# 32; (without the space) or something similar (%20?).
    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.


  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QDomDocument Eats Up My Spaces

    Quote Originally Posted by tbscope View Post
    An XML parser should always present the data unchanged to the user, including white spaces.
    Quote Originally Posted by W3C
    An XML processor MUST always pass all characters in a document that are not markup through to the application.
    and
    Definition: A software module called an XML processor is used to read XML documents and provide access to their content and structure.
    This is an XML reader (XML processor in their terminology) requirement. Doesn't say anything about writers that I can find.

    The collapsing of the space may also be the result of an explicit normalize() call.

    Following from Wysota's post, adding the xml:space attribute to the elements may stop the loss of the space in the writer:
    Qt Code:
    1. <container>
    2. <datavalue xml:space="preserve"> </datavalue>
    3. </container>
    To copy to clipboard, switch view to plain text mode 
    You may need to declare the attribute (see tbscope's link).

  8. #8

    Default Re: QDomDocument Eats Up My Spaces

    Thanks for the info, I'll try adding that attribute if it helps.

  9. #9
    Join Date
    Jun 2009
    Posts
    33
    Thanks
    5
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QDomDocument Eats Up My Spaces

    O.k. so did it help?

Similar Threads

  1. QTextDocument eats whitespacess
    By Eol in forum Qt Programming
    Replies: 5
    Last Post: 8th June 2010, 12:15
  2. Spaces in QTextEdit
    By jgrauman in forum Qt Programming
    Replies: 2
    Last Post: 22nd June 2009, 19:28
  3. Split QDomDocument to new QDomDocument
    By estanisgeyer in forum Qt Programming
    Replies: 4
    Last Post: 28th January 2009, 10:59
  4. QDomDocument inside other QDomDocument
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 13th November 2008, 16:27
  5. qmake INCLUDEPATH with spaces
    By bitChanger in forum Qt Programming
    Replies: 8
    Last Post: 28th April 2006, 06:39

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.