887

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

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

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

2
  • 13
    You should know that comments within an xml file are considered nodes of XmlComment type. So if you load the xml file those comments nodes are going to get loaded and it is up to you to avoid them or filter them when parsing the loaded content.
    – El Bayames
    Jul 18, 2013 at 14:14
  • XML comments are similar to comments in HTML. Aug 22, 2016 at 11:38

9 Answers 9

1261

You can use that style of comment across multiple lines (which exists also in 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>
7
  • 69
    One caveat with this is that you will have problems with nested comments. You will have to either: (1) remove the trailing ">" on the nested comment's close, or (2) remove the nested comments altogether. Mar 6, 2013 at 17:50
  • 1
    I have run into problems with (1), as some XML readers (i.e. CruiseControl.NET) may have problems reading a nested comment that has its ">" removed from the end. I ended up having to remove the comments altogether. Mar 6, 2013 at 17:52
  • 17
    @coderob Actually, even -- is not allowed within XML comments. So you might have to remove the entire -->
    – 0fnt
    Sep 30, 2014 at 13:21
  • In the Android Studio, select the block, then Ctrl + Slash to comment it (or Ctrl + Shift + Slash). Jul 20, 2016 at 22:33
  • 1
    One can run into problems using -- within this type of comment. Better to use - -> if you need to temporarily nest a comment. In HTML anyway (a subset of xml), including -- inside a comment is not valid. Usually you can get away with it, but does sometimes cause a problem. So, I be sure to stay clear of multiple - in a row within comments, and if I need to temp nest a comment, I'll place spaces between the 2 closing -- of the -->. This avoids randomly odd errors in XML and HTML. Dec 8, 2018 at 23:24
207

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)

2
  • 15
    This method worked exactly how I needed it to, and had the added advantage of working even around internal comments. I'd use this over the accepted answer if you have any form of complicated code. Mar 8, 2016 at 18:50
  • 4
    This even works with malformed XML inside. So this is a great solution for temporarily commenting out a block.
    – Max
    Aug 28, 2017 at 17:48
149

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 should not make a difference, but ideal and real world can be quite a bit apart, sometimes (especially when it comes to XML processing).

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:

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

3
  • 1
    Yes... I always found SGML and XML parsing of comments hard to remember all of the quirks... May 3, 2010 at 11:15
  • 1
    Thanks a lot for mentioning the strange fact with the double hyphens --! I had a case where I commented out a comment. Although I removed the old comment ending, it failed. Example: <!-- ...<code><!-- Old comment </code> ... -->
    – dwettstein
    Jun 3, 2015 at 11:30
  • 1
    To workaround the double-hyphen limitation you can replace -- with -&#45;. It should work the same after uncommenting in most cases.
    – mik
    Feb 6, 2019 at 20:00
54

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

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

Here for commenting we have to write like below:

<!-- Your comment here -->

Shortcuts for IntelliJ Idea and Eclipse

For Windows & Linux:

Shortcut for Commenting a single line:

Ctrl + /

Shortcut for Commenting multiple lines:

Ctrl + Shift + /

For Mac:

Shortcut for Commenting a single line:

cmnd + /

Shortcut for Commenting multiple lines:

cmnd + Shift + /

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

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

Here, TextView is a XML Tag and text is 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
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Hello.."
    android:textStyle="bold" />-->
0
13

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.

11

Syntax for 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 Comments Rules

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

In Notepad++ you can select few lines and use CTRL+Q which will automaticaly make block comments for selected lines.

-1

If you are using Eclipse IDE you can comment out lines in an XML file by highlighting them and press Ctrl+Shift+c.

Not the answer you're looking for? Browse other questions tagged or ask your own question.