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

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

9 Answers

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

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 )

2

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

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

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

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

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.

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. 

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

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

You Might Also Like