使用 QXmlStreamWriter 进行多行注释

发布于 2024-10-05 14:07:49 字数 2010 浏览 0 评论 0原文

我正在尝试使用 qxmlstreamwriter 将 xml 的多行部分注释到输出文件。我处于循环中,迭代嵌套结构,如果结构被注释为“isCommented”,那么我需要插入“< ! - -”(不带空格) 然后继续编写输出的 XML 形式。当我到达该结构的末尾时,我需要插入结束注释:“-->”。 qxmlstreamwriter::writeCharacters(QString) 方法还不够,因为它会挑选出特殊字符,例如“<”并重新解释它们。我已经处理了消除嵌套注释的情况...所以这不是问题(保证内部和外部循环不会同时被注释)对于替代解决方案有什么想法吗?下面是我的代码示例:

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

感谢您抽出时间。

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.writeEndElement();

  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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里°也失望 2024-10-12 14:07:49

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文