So How Do I Render HTML Inside XML Tag?

V

vunet.us

My IE XML parser is using property similar to
xmlObj.childNodes[0].text to get the value inside of my XML object it
is looping through. Now I place some HTML inside of XML tag but I am
unable to see any HTML retrieved as if it never was there. Please,
suggest me something other than converting <> to [] in my xml file.
Playing with escape/unescape did not help too.
Thanks to all.
 
J

Jeremy

My IE XML parser is using property similar to
xmlObj.childNodes[0].text to get the value inside of my XML object it
is looping through. Now I place some HTML inside of XML tag but I am
unable to see any HTML retrieved as if it never was there. Please,
suggest me something other than converting <> to [] in my xml file.
Playing with escape/unescape did not help too.
Thanks to all.

A little more detail in your question (e.g. sample code) would be
helpful, but I think I can guess what your problem is.

Any tags in your XML document, whether or not you want them to, are
going to be interpreted as XML nodes (as an aside, it is actually
surprising that your XML document was successfully parsed if you are
using HTML inside of it).

So you have two options to make your HTML bits get interpreted as text
nodes rather than XML fragments:

Option 1: In your HTML fragments, replace all XML special characters
(e.g. "<", ">", """, and "&") with their corresponding XML entities
(e.g. "&lt;", "&gt;", "&quot;", and "&amp;", respectively). So this:

<xmlNode>
<a href="http://www.w3.org">W3C Website</a>
</xmlNode>

becomes this:

<xmlNode>
&lt;a href=&quot;http://www.w3.org&quot;&gt;W3C Website&lt;/a&gt;
</xmlNode>


Option 2: Encase your HTML fragments in CDATA sections, which direct the
XML parser to interpret them as literals rather than nodes. This is
most likely the best option for you. Using the same example, you would
end up with:

<xmlNode><![CDATA[
<a href="http://www.w3.org">W3C Website</a>
]]></xmlNode>

A block of <![CDATA[...]]> can contain anything, except the literal
"]]>" which would obviously close the CDATA section.

Jeremy
 
V

vunet.us

My IE XML parser is using property similar to
xmlObj.childNodes[0].text to get the value inside of my XML object it
is looping through. Now I place some HTML inside of XML tag but I am
unable to see any HTML retrieved as if it never was there. Please,
suggest me something other than converting <> to [] in my xml file.
Playing with escape/unescape did not help too.
Thanks to all.

A little more detail in your question (e.g. sample code) would be
helpful, but I think I can guess what your problem is.

Any tags in your XML document, whether or not you want them to, are
going to be interpreted as XML nodes (as an aside, it is actually
surprising that your XML document was successfully parsed if you are
using HTML inside of it).

So you have two options to make your HTML bits get interpreted as text
nodes rather than XML fragments:

Option 1: In your HTML fragments, replace all XML special characters
(e.g. "<", ">", """, and "&") with their corresponding XML entities
(e.g. "&lt;", "&gt;", "&quot;", and "&amp;", respectively). So this:

<xmlNode>
<a href="http://www.w3.org">W3C Website</a>
</xmlNode>

becomes this:

<xmlNode>
&lt;a href=&quot;http://www.w3.org">W3CWebsite&lt;/a&gt;
</xmlNode>

Option 2: Encase your HTML fragments in CDATA sections, which direct the
XML parser to interpret them as literals rather than nodes. This is
most likely the best option for you. Using the same example, you would
end up with:

<xmlNode><![CDATA[
<a href="http://www.w3.org">W3C Website</a>
]]></xmlNode>

A block of <![CDATA[...]]> can contain anything, except the literal
"]]>" which would obviously close the CDATA section.

Jeremy

Jeremy, use of CDATA was a perfect solution for me. A great answer.
Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,182
Latest member
BettinaPol

Latest Threads

Top