Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to create CDATA section in XML using "xmlbuilder" node.js module

I am using "xmlbuilder" node.js module to create xml file. I need to create a CDATA section as follows:

<notestext><![CDATA[{Notes Text}]]></notestext>

I referred the github link, but didn't found any useful stuff.

How to create such CDATA section in an xml file using "xmlbuilder" node.js module?

let builder = require('xmlbuilder', { encoding: 'utf-8' });
let xml = builder.create('Slides');
xml.ele("notestext","<![CDATA[" + element.notes_text + "]]>");
xml.end({ pretty: true });

console.log(xml.toString());
like image 236
Kushagra Sinha Avatar asked Dec 12 '25 06:12

Kushagra Sinha


1 Answers

From Doc you posted

CDATA Nodes CDATA nodes are created with the cdata function (can also be abbreviated to dat or d). The value should not include CDATA delimiters

ele.dat('this will be surrounded by CDATA delimiters');
var builder = require('xmlbuilder', { encoding: 'utf-8' });

var xml = builder.create('slides');
xml.ele('notestext').dat('{Notes Text}');

xml.end({ 
  pretty: true,
  indent: '  '
});

console.log(xml.toString());
like image 98
Redanium Avatar answered Dec 13 '25 20:12

Redanium



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!