Results 1 to 5 of 5

Thread: Adding whitespace in QDomDocument

  1. #1
    Join Date
    Jan 2006
    Posts
    17
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Adding whitespace in QDomDocument

    I'm using QT 3.3.1 on Sun/Windows XP and creating an output file in XML using QDomDocument. I'm beginning the file using a couple of comments and then appending the comment to the document.

    Qt Code:
    1. QDomElement myElement;
    2. QDomComment myComment = myDoc.createComment( "This is the first comment." );
    3. myDoc.appendChild( myComment );
    4. myComment = myDoc.createComment( "This is the second comment." );
    5. myDoc.appendChild( myComment );
    6. while (anotherElementToAdd)
    7. {
    8. /* I add a bunch of elements to the document */
    9. }
    To copy to clipboard, switch view to plain text mode 

    I then use toString( 4 ) to output the file as a string and then send it to an ofstream file to save it to a file. However, the output of the file has the two comments along with the first element all on the same line. Is there a way to force a carriage return using QDomDocument?

    Thanks for your help.
    The end of time is reached around the year 8000, by which time we expect Qt to be obsolete.

    -Note in QDate documentation. It's nice that Qt has been able to predict when the world will end.

  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: Adding whitespace in QDomDocument

    Well I don't see you add the element to the tree, it is just hanging there alone... and the document should have some kind of root element too... Anyway, try adding newlines '\n' to your comments. I guess DOM encodes those comments as CDATA fields, so you can do whatever you want in them.

  3. #3
    Join Date
    Jan 2006
    Posts
    17
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Adding whitespace in QDomDocument

    Quote Originally Posted by wysota
    Well I don't see you add the element to the tree, it is just hanging there alone... and the document should have some kind of root element too... Anyway, try adding newlines '\n' to your comments. I guess DOM encodes those comments as CDATA fields, so you can do whatever you want in them.
    The problem with adding the \n to the comments is that the end of the comment ends up being right in frong of the next tag.

    Qt Code:
    1. <!--?? This is the first comment
    2. --><!--?? This is the second comment
    3. --><element>
    To copy to clipboard, switch view to plain text mode 

    I want it to look like this:

    Qt Code:
    1. <!--?? This is the first comment-->
    2. <!--?? This is the second comment-->
    3. <element>
    To copy to clipboard, switch view to plain text mode 

    Is there any way to force carriage returns in between tags?
    The end of time is reached around the year 8000, by which time we expect Qt to be obsolete.

    -Note in QDate documentation. It's nice that Qt has been able to predict when the world will end.

  4. #4
    Join Date
    Jan 2006
    Posts
    17
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Adding whitespace in QDomDocument

    A new thought occurred to me:

    Would it work to take the QString output from QDomDocument and replace --> with -->\n?

    EDIT: I was hoping that there was a very easy way to do this with QDomDocument instead of replacing strings, which feels like a bit of a kludge.
    The end of time is reached around the year 8000, by which time we expect Qt to be obsolete.

    -Note in QDate documentation. It's nice that Qt has been able to predict when the world will end.

  5. #5
    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: Adding whitespace in QDomDocument

    Quote Originally Posted by jakamph
    Would it work to take the QString output from QDomDocument and replace --> with -->\n?
    Yes, of course. Just be sure you are replacing the ending comment tag and not some arrow-like thing in CDATA section.

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.