951

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
  • 15
    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. Commented Jul 18, 2013 at 14:14
  • XML comments are similar to comments in HTML. Commented Aug 22, 2016 at 11:38

9 Answers 9

1342

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>
Sign up to request clarification or add additional context in comments.

7 Comments

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.
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.
@coderob Actually, even -- is not allowed within XML comments. So you might have to remove the entire -->
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.
For anyone wondering, technically XML does not state that nested comments are forbidden, it actually states that using -- in comments is not allowed (which causes problems with nested comments). This limitation is due to compatibility reasons with SGML, which uses -- as the comment syntax. Sources: w3.org/TR/xml/#sec-comments and w3.org/TR/xml/#dt-compat
|
227

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)

7 Comments

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.
This even works with malformed XML inside. So this is a great solution for temporarily commenting out a block.
Nice. And it's left alone by some source formatter (e.g. Eclipse), unlike multiline <!-- -->
Weird things happen with line endings though after saving, at least in .net. Just like <!-- -->
Is this nestable?
It is! It doesn't appear to work without a line break, though.
It does. There might be some exceptional symbols or strings which break it, but it's worked in 99% of the cases I've tried it with. I've utilised this to form the basis of stackoverflow.com/a/79735908/9731176.
156

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 Comments

Yes... I always found SGML and XML parsing of comments hard to remember all of the quirks...
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> ... -->
To workaround the double-hyphen limitation you can replace -- with -&#45;. It should work the same after uncommenting in most cases.
60

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

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

1 Comment

43

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

Comments

17

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

E.g.

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

Comments

14

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.

1 Comment

0

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

1 Comment

In oXygen it's CMD+SHIFT+M on the Mac. Most XML editors should offer something similar.
-4

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.