KT B
KT B

Reputation: 135

NodeJS xml2js - removes CDATA tag while converting from XML to JSON

In NodeJS by using xml2js module I am converting the XML string to JSON object and after some edit again converting that JSON object back into XML. All this is working well however the problem is that CDATA tags are missing in the converted XML. Can someone help me with this? I am giving the sample code below which has the same issue.

var xml2js = require('xml2js');
var parser = new xml2js.Parser();
parser.parseString("<myxml myattribute='value'><![CDATA[Hello again]]>
</myxml>", function (err, data) {   

var builder = new xml2js.Builder({
cdata: true
});
var xml = builder.buildObject(data);
 console.log(" ------------ "+xml);
});

Thanks -kt

Upvotes: 2

Views: 3181

Answers (1)

Cody Geisler
Cody Geisler

Reputation: 8617

Please read https://github.com/Leonidas-from-XIV/node-xml2js/issues/218

Per the package author, per wikipedia:

A CDATA section is merely an alternative syntax for expressing character data; there is no semantic difference between character data that manifests as a CDATA section and character data that manifests as in the usual syntax in which "<" and "&" would be represented by "<" and "&", respectively.

The documentation states for the option cdata:

cdata (default: false): wrap text nodes in instead of escaping when necessary. Does not add if it is not required. Added in 0.4.5.

Upvotes: 3

Related Questions