Jump to content

Error when sending HTML code using XML


swiftaxe

Recommended Posts

I'm storing html-code in a database.
I use an Ajax request to fetch the code.
Then I return the data using xml.

The problem occurs when the XML is created.
If there is a tag without an endtag, like <img>, in the html code to be returned, then an xml error occurs.
[b]The html tags within the code is interpreted as xml tags[/b], and thus requires that all tags have an end tag.
:-\

[b]Example:[/b]
[code]<?xml version="1.0" encoding="UTF-8" ?>
<data>
  <code><p>Example</p><img src="anything"></code>
</data>
[/code]

Is there an easy way around this problem?

Thanks
Link to comment
Share on other sites

  • 4 weeks later...
Hi.

Yes, "<" and "&" are illegal in XML. You'll need to make use of the CDATA tag in order to use HTML and such inside an XML section. Everything inside a CDATA tag is ignored by the parser.

For example:

[code]
<?xml version="1.0" encoding="UTF-8" ?>
<data>
  <code>
    <![CDATA[<p>Example</p><img src="anything" />]]>
  </code>
</data>
[/code]

You should also be aware that you can't actually put "]]>" inside a CDATA tag for obvious reaons.

The CDATA tag starts with <![CDATA[ and ends with ]]>
Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.