tags:

views:

134

answers:

2

Hi, I am very very new to xml. I am developing an application which uses xml file. I created this file with the help of Google. My sample xml file is:

<?xml version="1.0"?>
<gamelist>
 <game>
  <title>Driver</title>
  <code><EMBED src="http://www.pnflashgames.com/modules/pnFlashGames/games/racer.swf"&gt;&lt;/EMBED&gt;&lt;/code&gt;
  <rating>4</rating>
 </game>
 <game>
  <title>ConeCrazy</title>
  <code><EMBED     src="http://www.pnflashgames.com/modules/pnFlashGames/games/ConeCrazy.swf"&gt;&lt;/EMBED&gt;&lt;/code&gt;
  <rating>3</rating>
 </game>
</gamelist>

In the above file code element is having embed tag. For my application I need embed tag as a string. In code element if I use any string instead of embed tag I can read that string. If I use embed tag I am getting error like this:

[fatal error] gamelist.xml:5:103 : reference to entity "pn_uname" must end with the ";" delimeter .

I am using Java for reading xml file. In my Java class I want the whole embed tag as a string.

+5  A: 

I suppose you are not generating the XML with suitable tools (a DOM API), but by concatenating strings.

If further suppose the XML you are showing in your question is not the one that triggers the error.

I think you have something like this:

<EMBED src="http://foo/bar/racer.swf?bla&amp;pn_uname=baz"&gt;&lt;/EMBED&gt;

This would trigger an error message stating that the character entity "pn_uname" is not well-formed.

The correct way to express the above would be:

<EMBED src="http://foo/bar/racer.swf?bla&amp;amp;pn_uname=baz"&gt;&lt;/EMBED&gt;

...which is what using an API for XML generation would automatically handle for you. Avoid string concatenation to produce XML.

Tomalak
+4  A: 

To put html code as a value in an XML element you have to encode it:

<?xml version="1.0"?>
<gamelist>
 <game>
  <title>Driver</title>
  <code>&lt;EMBED src=&quot;http://www.pnflashgames.com/modules/pnFlashGames/games/racer.swf&amp;quot;&amp;gt;&amp;lt;/EMBED&amp;gt;&lt;/code&gt;
  <rating>4</rating>
 </game>
 <game>
  <title>ConeCrazy</title>
  <code>&lt;EMBED src=&quot;http://www.pnflashgames.com/modules/pnFlashGames/games/ConeCrazy.swf&amp;quot;&amp;gt;&amp;lt;/EMBED&amp;gt;&lt;/code&gt;
  <rating>3</rating>
 </game>
</gamelist>

List of: entities in XML

Guffa
Hm... wouldn't it also be possible to keep the file as it is? As long as it is XHTML, there is no need to encode.
Tomalak
Yes, it's possible, but then you would have to get the innerXml of the code element instead of it's value.
Guffa
But it will bite you when you try to do XPath/XSLT with the document. I would always try to store XHTML directly (if necessary convert it to XHTML first).
Tomalak
Also, the fact that EMBED is in uppercase seems to indicate it is not XHTML.
bortzmeyer