2010-12-01 123 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