Multiline comment with QXmlStreamWriter
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 "< ! - -" (without spaces) 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.writeEndE开发者_如何转开发lement();
if (bigStruct.at(i)->isCommented){
//endcomment sequence
//insert "-->"
}
}
...
An example XML output 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>
-->
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. Therefore I could write XML using my qxmlstreamwriter inside a comment.
Thanks for your time.
I don't think that's possible, looking at the QXmlStreamWriter API. You could use another QXmlStreamWriter on a temporary QByteArray, and write the byte array's content as comment to the original writer.
精彩评论