3

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());
0

2 Answers 2

5

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());
Sign up to request clarification or add additional context in comments.

2 Comments

No such method found error is coming when applying "dat()" method. Please check
it worked for me [email protected] node v6.9.4 npm v3.10.10
2

Solution 1:

Utilize the .cdata (.dat, or .d) method and chain each method to generate the XML fragment. For instance:

Javascript:

var builder = require('xmlbuilder');

var element = {
  notes_text: '<p>Hello <em>World</em></p>'
};

var xml = builder
    .create('slides', { version: '1.0', encoding: 'UTF-8', standalone: true })
    .ele('notestext')
    .cdata(element.notes_text)
    .end({ 
      pretty: true
    });

console.log(xml);

Output:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<slides>
  <notestext>
    <![CDATA[<p>Hello <em>World</em></p>]]>
  </notestext>
</slides>

Solution 2:

Another way to write it, which is more similar to your example, is as follows:

var builder = require('xmlbuilder', { encoding: 'utf-8' });

var element = {
  notes_text: '<p>Hello <em>World</em></p>'
};

var xml = builder.create('slides');
xml.ele('notestext').cdata(element.notes_text);

xml.end({ 
  pretty: true
});

console.log(xml.toString());

Note: This example uses less method chaining than the previous example, however it does chain the cdata method to the ele method.

This prints the following:

<slides>
  <notestext>
    <![CDATA[<p>Hello <em>World</em></p>]]>
  </notestext>
</slides>

Solution 3:

Alternatively, if you don't want to chain any methods you can do something like following:

var builder = require('xmlbuilder');

var element = {
  notes_text: '<p>Hello <em>World</em></p>'
};

var rootElement = builder.create('slides');

var childElement = rootElement.ele('notestext')
childElement.cdata(element.notes_text);

rootElement.end({ 
  pretty: true
});

console.log(rootElement.toString());

This also prints the same output as Solution 2.


Additional information:

The docs describe the .cdata method as follows:

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');


Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.