hanu
hanu

Reputation: 61

how to place < symbol in xml file which is to be read by the java program?

I am placing an SQL query(which contains < symbol) inside xml file and i am trying to read that query in a java program. But it is displaying the exception

"org.xml.sax.SAXParseException: The content of elements must consist of well-formed character data or markup."

can any one help me how to fix the above issue?

Upvotes: 3

Views: 1507

Answers (3)

Pranay Rana
Pranay Rana

Reputation: 176936

Make use of CDATA

CDATA - (Unparsed) Character Data

CDATA stands for Character Data and it means that the data in between these tags includes data that could be interpreted as XML markup, but should not be

The term CDATA is used about text data that should not be parsed by the XML parser.

Characters like "<" and "&" are illegal in XML elements.

"<" will generate an error because the parser interprets it as the start of a new element.

"&" will generate an error because the parser interprets it as the start of an character entity.

Some text, like JavaScript code, contains a lot of "<" or "&" characters. To avoid errors script code can be defined as CDATA.

Everything inside a CDATA section is ignored by the parser.

Example:

<![CDATA[ select <abcddata> ]]>

Upvotes: 4

Manuel
Manuel

Reputation: 10303

< = &gt;
> = &lt;

These are the HTML entities and should be accepted

Upvotes: 3

Bohemian
Bohemian

Reputation: 425128

You need to escape using XML entities:

  • & encode as &amp;
  • < encode as &lt;

Technically, you don't need to escape the following, but it is common to do so:

  • > encode as &gt;
  • " encode as &quot;
  • ' encode as &apos;

For more info, see this Wikipedia article for more

Upvotes: 6

Related Questions