I'm trying to build a XML node with xml2js. When I add html tags wrapped in cdata, it creates a separate node as shown below. And if I wrap the CDATA inside "<>" the value inside gets undefined. Basically I'm trying to create XML documents from which I can take the node values and show it in the HTML documents using jQuery/Angular. And those node must be able to contain HTML tags
var xml2js = require('xml2js');
var parser = new xml2js.Parser({
explicitArray: false
});
var builder = new xml2js.Builder({
cdata: true
});
var test = "<parent>![CDATA[Hey There! <span> Buddy.</span>]]</parent>"
parser.parseString(test, function(err, result) {
var xml = builder.buildObject(result)
console.log(xml)
});
//Output
/*
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<parent>
![CDATA[Hey There! ]]
<span> Buddy.</span>
</parent>
*/
var test2 = "<parent><![CDATA['Hey There! <span> Buddy.</span>']]></parent>";
parser.parseString(test, function(err, result) {
var xml = builder.buildObject(result)
console.log(xml)
});
//Output :
/*
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<parent><![CDATA[undefined]]></parent>
*/