PDA

View Full Version : Multiline comment with QXmlStreamWriter



QtWannabe
24th November 2010, 17:57
Hello all,

I am trying comment a multiline section of xml to an output file using qxmlstreamwriter. I am in a loop, iterating through my nested structures, and if a structure is noted as "isCommented" then I need to insert a "<!--" then continue writing the XML form of the output. When I get to the end of that structure I need to insert the end comment "-->". The qxmlstreamwriter::writeCharacters(QString) method won't suffice since it picks out the special characters such as "<" and re-interprets them. I have already handled eradicating cases for nested comments... so that isn't an issue (the inner and outer loop are guaranteed to not both be commented) Any ideas for an alternate solution? Below is an example of my code:



...
QXmlStreamWriter writer(&myFile)

for (int i = 0; i < bigStruct.size(); i++){

if (bigStruct.at(i)->isCommented){
//start comment sequence
//insert "<!--"
}

writer.writeStartElement("BigStruct");

for (int j = 0; j < smallerStruct; j++){
if (smallerStruct.at(i)->isCommented){
//start comment sequence
//insert "<!--"
}

writer.writeStartElement("SmallerStruct");

writer.writeTextElement("Stuff", "blah");
writer.writeTextElement("More Stuff", "blah blah blah");

writer.writeEndElement();

if (smallerStruct.at(i)->isCommented){
//end comment sequence
//insert "-->"
}
}

writer.writeEndElement();

if (bigStruct.at(i)->isCommented){
//endcomment sequence
//insert "-->"
}
}

...


An example XML may look like:


<BigStruct>
<SmallerStruct>
<Stuff>blah</Stuff>
<More Stuff>blah blah blah</More Stuff>
</SmallerStruct>
<!--
<SmallerStruct>
<Stuff>blah</Stuff>
<More Stuff>blah blah blah</More Stuff>
</SmallerStruct>
-->
</BigStruct>
<!--
<BigStruct>
<SmallerStruct>
<Stuff>blah</Stuff>
<More Stuff>blah blah blah</More Stuff>
</SmallerStruct>
</BigStruct>
-->



Thanks for your time.

tbscope
25th November 2010, 05:47
http://doc.qt.nokia.com/4.7/qxmlstreamwriter.html#writeComment

QtWannabe
30th November 2010, 13:05
Thanks for your time and reply.

The writeComment() was my first attempt to comment, however, it only writes a single line. Of course, I could create a large string with '\n' characters where necessary, but the code necessary to create that block would escape the program flow of my loops. What I need is a slick way of essentially having a writer.startComment() and writer.endComment()... such that I can specify the start and end of a comment, after other xml has been written. Therefor I could write XML using my qxmlstreamwriter inside a comment.