user1002782
user1002782

Reputation: 299

How to encode '<' & '>' Symbols in XML

Since < & > as part of XML syntax. How to add these symbols as part of the data like

<Note>Have to have <> symbols</Note>

I have heard there are different types of data in which XML can be sent like CDATA etc, since character data parses each character it doesn't allow these symbols.

I do know about &lt; and &gt; but that isn't helpful.

Are there are any modes of data in which XML can be sent to avoid any tool recognizing the symbols?

Upvotes: 4

Views: 21956

Answers (3)

phihag
phihag

Reputation: 287745

< is encoded as &lt;.
> is encoded as &gt;.
" is encoded as &quot;.
& is encoded as &amp;.

Alternatively, you can pack the whole data in a CDATA section if it doesn't contain a CDATA section itself. If you're generating programatically, encoding each character is the better solution though.

Note that there is a plethora of XML libraries available for almost every language. Unless you want to learn about XML, I strongly recommend using an XML library instead of writing your own.

Upvotes: 14

Quentin
Quentin

Reputation: 943099

There are two ways to represent characters which have special meaning in XML (such as < and >) in an XML document.

  1. CDATA sections
  2. As entities

A CDATA section can only be used in places where you could have a text node.

<foo><![CDATA[Here is some data including < and > (and &!) ]]></foo>

The caveat is that you can't include the sequence ]]> as data in a CDATA section.

Entities can be used everywhere (except inside CDATA sections) and consist of &, then an identifier, then ;.

<foo>Here is some data including &lt; and &gt; (and &amp;!)</foo>

I do know about &lt; and &gt; but that isn't helpful.

It is how XML works. It is one of your only two options. It should not be a problem (if it is, then you need to explain why in your question).

Upvotes: 7

Fischermaen
Fischermaen

Reputation: 12458

You can encode as &amp; entity

Upvotes: 1

Related Questions