Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to insert HTML content in XML document?

Tags:

html

xml

I need to insert HTML content into an XML document, is this possible or should HTML content be, for example, encoded in BASE64 or with something else like that?

like image 474
newbie Avatar asked Dec 10 '10 19:12

newbie


People also ask

Can we use HTML in XML?

When displaying data in HTML, you should not have to edit the HTML file when the data changes. With XML, the data can be stored in separate XML files. With a few lines of JavaScript code, you can read an XML file and update the data content of any HTML page.


4 Answers

You can include HTML content. One possibility is encoding it in BASE64 as you have mentioned.

Another might be using CDATA tags.

Example using CDATA:

<xml>
    <title>Your HTML title</title>
    <htmlData><![CDATA[<html>
        <head>
            <script/>
        </head>
        <body>
        Your HTML's body
        </body>
        </html>
     ]]>
    </htmlData>
</xml>

Please note:

CDATA's opening character sequence: <![CDATA[

CDATA's closing character sequence: ]]>

like image 57
Pablo Santa Cruz Avatar answered Oct 16 '22 18:10

Pablo Santa Cruz


so long as your html content doesn't need to contain a CDATA element, you can contain the HTML in a CDATA element, otherwise you'll have to escape the XML entities.

<element><![CDATA[<p>your html here</p>]]></element>

VS

<element>&lt;p&gt;your html here&lt;/p&gt;</element>
like image 35
zzzzBov Avatar answered Oct 16 '22 17:10

zzzzBov


The purpose of BASE64 encoding is to take binary data and be able to persist that to a string. That benefit comes at a cost, an increase in the size of the result (I think it's a 4 to 3 ratio). There are two solutions. If you know the data will be well formed XML, include it directly. The other, an better option, is to include the HTML in a CDATA section within an element within the XML.

like image 8
Rich Avatar answered Oct 16 '22 17:10

Rich


Please see this.

Text inside a CDATA section will be ignored by the parser.

http://www.w3schools.com/xml/dom_cdatasection.asp

This is will help you to understand the basics about XML

like image 2
loyola Avatar answered Oct 16 '22 17:10

loyola