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









up vote
0
down vote

favorite












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









share|improve this question



























    up vote
    0
    down vote

    favorite












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









    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











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









      share|improve this question















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






      javascript node.js xml npm xml-builder






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 20:50









      RobC

      5,26592033




      5,26592033










      asked Nov 10 at 11:17









      Kushagra Sinha

      164




      164






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













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





          share|improve this answer






















          • No such method found error is coming when applying "dat()" method. Please check
            – Kushagra Sinha
            Nov 10 at 17:32










          • it worked for me xmlbuilder@10.1.1 node v6.9.4 npm v3.10.10
            – Redanium
            Nov 10 at 19:35

















          up vote
          0
          down vote













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









          share|improve this answer






















          • Thanks a lot brother. It worked for me.
            – Kushagra Sinha
            Nov 11 at 4:01










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238406%2funable-to-create-cdata-section-in-xml-using-xmlbuilder-node-js-module%23new-answer', 'question_page');

          );

          Post as a guest






























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













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





          share|improve this answer






















          • No such method found error is coming when applying "dat()" method. Please check
            – Kushagra Sinha
            Nov 10 at 17:32










          • it worked for me xmlbuilder@10.1.1 node v6.9.4 npm v3.10.10
            – Redanium
            Nov 10 at 19:35














          up vote
          1
          down vote













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





          share|improve this answer






















          • No such method found error is coming when applying "dat()" method. Please check
            – Kushagra Sinha
            Nov 10 at 17:32










          • it worked for me xmlbuilder@10.1.1 node v6.9.4 npm v3.10.10
            – Redanium
            Nov 10 at 19:35












          up vote
          1
          down vote










          up vote
          1
          down vote









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





          share|improve this answer














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






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 19:36

























          answered Nov 10 at 17:23









          Redanium

          745413




          745413











          • No such method found error is coming when applying "dat()" method. Please check
            – Kushagra Sinha
            Nov 10 at 17:32










          • it worked for me xmlbuilder@10.1.1 node v6.9.4 npm v3.10.10
            – Redanium
            Nov 10 at 19:35
















          • No such method found error is coming when applying "dat()" method. Please check
            – Kushagra Sinha
            Nov 10 at 17:32










          • it worked for me xmlbuilder@10.1.1 node v6.9.4 npm v3.10.10
            – Redanium
            Nov 10 at 19:35















          No such method found error is coming when applying "dat()" method. Please check
          – Kushagra Sinha
          Nov 10 at 17:32




          No such method found error is coming when applying "dat()" method. Please check
          – Kushagra Sinha
          Nov 10 at 17:32












          it worked for me xmlbuilder@10.1.1 node v6.9.4 npm v3.10.10
          – Redanium
          Nov 10 at 19:35




          it worked for me xmlbuilder@10.1.1 node v6.9.4 npm v3.10.10
          – Redanium
          Nov 10 at 19:35












          up vote
          0
          down vote













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









          share|improve this answer






















          • Thanks a lot brother. It worked for me.
            – Kushagra Sinha
            Nov 11 at 4:01














          up vote
          0
          down vote













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









          share|improve this answer






















          • Thanks a lot brother. It worked for me.
            – Kushagra Sinha
            Nov 11 at 4:01












          up vote
          0
          down vote










          up vote
          0
          down vote









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









          share|improve this answer














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










          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 22:42

























          answered Nov 10 at 18:08









          RobC

          5,26592033




          5,26592033











          • Thanks a lot brother. It worked for me.
            – Kushagra Sinha
            Nov 11 at 4:01
















          • Thanks a lot brother. It worked for me.
            – Kushagra Sinha
            Nov 11 at 4:01















          Thanks a lot brother. It worked for me.
          – Kushagra Sinha
          Nov 11 at 4:01




          Thanks a lot brother. It worked for me.
          – Kushagra Sinha
          Nov 11 at 4:01

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238406%2funable-to-create-cdata-section-in-xml-using-xmlbuilder-node-js-module%23new-answer', 'question_page');

          );

          Post as a guest














































































          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3

          Museum of Modern and Contemporary Art of Trento and Rovereto