如何注释掉 XML 中的标记块?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2757396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:04:46  来源:igfitidea点击:

How do I comment out a block of tags in XML?

xmlcomments

提问by Jonas

How do I comment out a block of tags in XML?

如何注释掉 XML 中的标记块?

I.e. How can I comment out <staticText>and everything inside it, in the code below?

即如何<staticText>在下面的代码中注释掉其中的所有内容?

  <detail>
    <band height="20">
      <staticText>
        <reportElement x="180" y="0" width="200" height="20"/>
        <text><![CDATA[Hello World!]]></text>
      </staticText>
    </band>
  </detail>

I could use <!-- staticText-->but that's just for single tags (as what I know), like //in Java and C. I would like something more like how /** comment **/can be used in Java and C, so I can comment out longer blocks of XML code.

我可以使用,<!-- staticText-->但这只是用于单个标签(据我所知),就像//在 Java 和 C 中一样。我想要更像如何/** comment **/在 Java 和 C 中使用的东西,所以我可以注释掉更长的 XML 代码块。

回答by Noon Silk

You can use that style of comment across multiple lines (which exists also in HTML)

您可以在多行中使用该样式的注释(也存在于 HTML 中)

<detail>
    <band height="20">
    <!--
      Hello,
         I am a multi-line XML comment
         <staticText>
            <reportElement x="180" y="0" width="200" height="20"/>
            <text><![CDATA[Hello World!]]></text>
          </staticText>
      -->
     </band>
</detail>

回答by Kasper van den Berg

You can wrap the text with a non-existing processing-instruction, e.g.:

您可以使用不存在的处理指令包装文本,例如:

<detail>
<?ignore
  <band height="20">
    <staticText>
      <reportElement x="180" y="0" width="200" height="20"/>
      <text><![CDATA[Hello World!]]></text>
    </staticText>
  </band>
?>
</detail>

Nested processing instructions are not allowed and '?>' ends the processing instruction (see http://www.w3.org/TR/REC-xml/#sec-pi)

不允许嵌套的处理指令,'?>' 结束处理指令(参见http://www.w3.org/TR/REC-xml/#sec-pi

回答by Boldewyn

If you ask, because you got errors with the <!-- -->syntax, it's most likely the CDATA section (and there the ]]>part), that then lies in the middle of the comment. It shouldnot make a difference, but ideal and real world can be quite a bit apart, sometimes (especially when it comes to XML processing).

如果你问,因为你的<!-- -->语法有错误,很可能是 CDATA 部分(还有那]]>部分),然后位于评论的中间。它不应该有什么不同,但有时(尤其是在涉及 XML 处理时),理想世界和现实世界可能会有很大的不同。

Try to change the ]]>, too:

也尝试更改]]>

  <!--detail>
    <band height="20">
      <staticText>
        <reportElement x="180" y="0" width="200" height="20"/>
        <text><![CDATA[Hello World!]--><!--]></text>
      </staticText>
    </band>
  </detail-->

Another thing, that comes to mind: If the content of your XML somewhere contains two hyphens, the comment immediately ends there:

我想到的另一件事是:如果某个地方的 XML 内容包含两个连字符,则评论会立即结束:

<!-- <a> This is strange -- but true!</a> -->
--------------------------^ comment ends here

That's quite a common pitfall. It's inherited from the way SGML handles comments. (Read the XML spec on this topic)

这是一个很常见的陷阱。它继承自 SGML 处理注释的方式。(阅读有关此主题的 XML 规范

回答by Delan Azabani

Actually, you can use the <!--...--> format with multi-lines or tags:

实际上,您可以将 <!--...--> 格式与多行或标签一起使用:

<!--
  ...
  ...
  ...
-->

回答by Avijit Karmakar

Here for commenting we have to write like below:

在这里评论我们必须写如下:

<!-- Your comment here -->

Shortcuts for IntelliJ Idea and Eclipse

IntelliJ Idea 和 Eclipse 的快捷方式

For Windows & Linux:

对于 Windows 和 Linux:

Shortcut for Commenting a single line:

注释单行的快捷方式:

Ctrl+ /

Ctrl+ /

Shortcut for Commenting multiple lines:

注释多行的快捷方式:

Ctrl+ Shift+ /

Ctrl+ Shift+/

For Mac:

对于 Mac:

Shortcut for Commenting a single line:

注释单行的快捷方式:

cmnd+ /

cmnd+ /

Shortcut for Commenting multiple lines:

注释多行的快捷方式:

cmnd+ Shift+ /

cmnd+ Shift+/

One thing you have to keep in mind that, you can't comment an attribute of an XML tag. For Example:

您必须记住的一件事是,您不能对 XML 标记的属性进行注释。例如:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    <!--android:text="Hello.."-->
    android:textStyle="bold" />

Here, TextViewis a XML Tag and textis an attribute of that tag. You can't comment attributes of an XML Tag. You have to comment the full XML Tag. For Example:

这里,TextView是一个 XML 标签,text是该标签的一个属性。您不能注释 XML 标记的属性。您必须注释完整的 XML 标记。例如:

<!--<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Hello.."
    android:textStyle="bold" />-->

回答by svg

You can easily comment out the data using this:

您可以使用以下方法轻松注释掉数据:

<!-- 
 <data>
        <data-field1></data-field1>
        <data-field2></data-field2>
        <data-field3></data-field3>
 </data>
-->

method of commenting in xml.

xml中注释的方法。

回答by TheSuMiT.

Syntax for XML : <!--Your comment-->

XML 的语法: <!--Your comment-->

eg.

例如。

   <?xml version = "1.0" encoding = "UTF-8" ?>
   <!--here is your comment :) -->
   <class_list>   
   <student>
   <name></name>
   <grade>A</grade>
   </student>
   </class_list>
   <?xml version = "1.0" encoding = "UTF-8" ?>
   <!--here is your comment :) -->
   <class_list>   
   <student>
   <name></name>
   <grade>A</grade>
   </student>
   </class_list>

XML Comments Rules

XML 注释规则

Comments cannot appear before XML declaration.
Comments may appear anywhere in a document.
Comments must not appear within attribute values.
Comments cannot be nested inside the other comments.