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
https://forums.phpfreaks.com/topic/30227-error-when-sending-html-code-using-xml/
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 ]]>
  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.