2010-12-01 45 views
1

我正在尝试使用qxmlstreamwriter将多行部分的xml注释到输出文件。我在一个循环中,遍历我的嵌套结构,如果一个结构被标记为“isCommented”,那么我需要插入一个“<! - - ”(不带空格) 然后继续编写输出的XML格式。当我到达该结构的末尾时,我需要插入结束注释:“ - >”。 qxmlstreamwriter :: writeCharacters(QString)方法不能满足要求,因为它会挑选诸如“<”之类的特殊字符并重新解释它们。我已经处理了根除嵌套注释的案例...所以这不是问题(内部和外部的回路不能同时被注释)任何替代解决方案的想法?下面是我的代码示例:使用QXmlStreamWriter多行注释

... 
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 "-->" 
    } 
} 

... 

一个例子XML输出可能看起来像

<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> 
--> 

的writeComment()是我的第一个留言的尝试,但是,只写一行。当然,我可以根据需要创建一个带有'\ n'字符的大字符串,但创建该块所需的代码将会跳过我的循环的程序流。 我需要的是基本上有一个writer.startComment()和writer.endComment()这样一个简单的方法,以便我可以在写入其他xml后指定注释的开始和结束。因此,我可以在评论中使用我的qxmlstreamwriter编写XML。

谢谢你的时间。

回答

0

我不认为这是可能的,看着QXmlStreamWriter API。您可以在临时QByteArray上使用另一个QXmlStreamWriter,并将字节数组的内容作为注释写入原始写入器。

+0

感谢您的回复。你知道任何其他可以支持writer.startComment()/ writer.endComment()功能的qXML解析器类吗? – GatorGuy 2010-12-02 13:33:15