PDA

View Full Version : Adding whitespace in QDomDocument



jakamph
6th February 2006, 16:54
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.


QDomDocument myDoc = QDomDocument( );
QDomElement myElement;
QDomComment myComment = myDoc.createComment( "This is the first comment." );
myDoc.appendChild( myComment );
myComment = myDoc.createComment( "This is the second comment." );
myDoc.appendChild( myComment );
while (anotherElementToAdd)
{
/* I add a bunch of elements to the document */
}


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.

wysota
6th February 2006, 18:35
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.

jakamph
6th February 2006, 20:20
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.


<!--?? This is the first comment
--><!--?? This is the second comment
--><element>


I want it to look like this:


<!--?? This is the first comment-->
<!--?? This is the second comment-->
<element>


Is there any way to force carriage returns in between tags?

jakamph
6th February 2006, 20:22
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.

wysota
6th February 2006, 23:06
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.