Writing a xml output with special character to a table

In my xml node, i have a node value with special character < and >
<MarkText><ASSY MARK></MarkText>

I can retrieve it using alert(MarkTextNodes.item(i).text);
However, when i try to write the output to a table :

strMarkObjectTable += "<table><tr><td>" + MarkTextNodes.item(i).text + "</tr></td></table>";

It is giving me an empty table. Please help!
Who is Participating?
Peter KwanConnect With a Mentor Analyst ProgrammerCommented:
You need to escape the < to &lt; and > to &gt; as the following:

strMarkObjectTable += "<table><tr><td>" + MarkTextNodes.item(i).text.replace("<", "&lt;").replace(">", "&gt;") + "</tr></td></table>";
sherlyAuthor Commented:
is there an option to add in the check for "&" as well?
Peter KwanAnalyst ProgrammerCommented:
Yes, you can replace & with &amp;. But remember to add as the first replace, otherwise, the < and > does not work.

What I mean is the following:

var x =  MarkTextNodes.item(i).text.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
sherlyAuthor Commented:
Thank you so much!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.