Javascript to add cdata section on the fly?

前端 未结 2 517
太阳男子
太阳男子 2021-01-24 17:54

I\'m having trouble with special characters that exist in an xml node attribute. To combat this, I\'m trying to render the attributes as child nodes and, where necessary, using

2条回答
  •  逝去的感伤
    2021-01-24 18:02

    I had the same issue. i was trying to append CDATA to xml nodes, so i thought its as easy as adding like so:

    valueNode[0].text = "";
    //valueNode[0] represents ""
    

    This does not work because the whole thing will get interpreted as text therefore <(less than) and > (great than) will be replaced automatically.

    what you need to do is use createCDATASection by doing the following:

    var tmpCdata = $xmlDoc[0].createCDATASection(escape("muzi test 002"));
    //i'm also escaping special charactures as well
    valueNode[0].appendChild(tmpCdata);
    

    results will be:

    
    

    Brettz9 (in previous answer) explains how to do this but quite complex, therefore i just wanted to add my solution which is much simpler.

    thanks,

提交回复
热议问题