Mahesh Gupta
Mahesh Gupta

Reputation: 925

Displaying data in XML

I'm writing an xml file. The file must contain the &q inside it and &y and &id. The problem is that when I'm opening the XML Viewer in Windows, it is giving me an error that semicolon is expected.

Is there any solution to this problem??? The element is:

<cs-uri-query> course=323-21-603&q=3&y=2002&id=671 </cs-uri-query>

Upvotes: 3

Views: 263

Answers (3)

missingfaktor
missingfaktor

Reputation: 92036

There are some characters that are reserved in XML. Ampersand is one of them. For displaying such characters, you need to use entity references.

Some commonly used entity references are as follows :

alt text http://lh4.ggpht.com/_oJlVatCN8NA/S0himi9xtZI/AAAAAAAAAU8/RiEyYow4ntA/Untitled.jpg

As can be seen in above table, entity reference for ampersand is &amp; .

Upvotes: 2

Tomalak
Tomalak

Reputation: 338208

"I'm writing an xml file."

No, you are not. You are writing a text file that you think looks like XML. Had you used an XML-aware tool to do it (i.e. a DOM API), you would not have asked this question, since the API would take care of these low-level problems.

There is an abundance of APIs, I'm sure there is one for your language of choice, too. To avoid this and a number of other subtle problems, I recommend you switch your code to using one.

Upvotes: 5

David M
David M

Reputation: 72870

You must escape the & to include it in XML, using &amp;. Alternatively, you can store it in a CDATA section. Examples:

<element>course=323-21-603&amp;q=3</element>

<element><![CDATA[course=323-21-603&q=3]]></element>

Upvotes: 2

Related Questions