How can I comment out a block of XML tags, such as <staticText> and its contents, using the correct XML comment syntax?

For example, given this code:

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

I know <!-- staticText -- > works for single tags, but how do I comment out an entire block like in Java’s /** ... */ style?

What’s the proper way to write an xml comment for multiple lines?

I had the same question when I first started working with XML. The good news is, XML doesn’t support Java-style block comments like /** ... */.

Instead, to comment out multiple lines, you wrap the entire block with the standard XML comment syntax:

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

Just make sure you don’t include – inside the comment, because that’s not allowed in XML comments.

I agree — XML comments can be tricky. The xml comment syntax only allows <!-- to start and --> to end the comment. So to comment out a block like <staticText>, you just enclose the whole section, like this:

<!--  
<staticText>  
  <reportElement .../>  
  <text>...</text>  
</staticText>  
-->

You can comment out multiple lines or tags this way, but remember that nested comments aren’t allowed. If your block has existing comments, you may need to remove or adjust them first.

Yep, the best way to comment out a block in XML is using the standard <!-- ... --> syntax around the whole block. XML doesn’t have multiline comment shortcuts like Java or C-style comments.

One thing to watch out for is not to have double hyphens -- inside the comment because it’s invalid. So for your example, just do:

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

This works perfectly to disable entire blocks without breaking your XML.