Node.js remove root node from resulting XML using xml2js












3















I'm trying creating a XML from JSON obj and its giving me root element in the result, I tried setting the explicitRoot var parser = xml2js.Parser({explicitRoot:false});
to false but it does not remove the default root tag but just removing my orignal XML root tag (<VSAT></VSAT>)



Processing XML using xml2js



<?xml version="1.0" encoding="utf-8"?>
<VAST version="2.0">
<Ad id="72419"></Ad>
</VAST>


Resulting XML:



<?xml version="1.0" encoding="utf-8"?>
<root>
<VAST version="2.0">
<Ad id="72419"></Ad>
</VAST>
</root>


Any idea ?



full code



/*
NodeJS server
*/
var http = require('http');
var xml2js = require('xml2js');
var fs = require('fs');
var util = require('util');
var json,PORT=2000;

var server = http.createServer(function(request, response){
response.writeHead(200,{'Content-Type':'text/html'});
try{
var filedata = fs.readFileSync('vast_all.xml','ascii');

var parser = xml2js.Parser({explicitRoot:true});
parser.parseString(filedata.substring(0,filedata.length),function(err,result){
result.new = 'test';
json = JSON.stringify(result);

var builder = new xml2js.Builder({
xmldec:{ 'version': '1.0', 'encoding': 'UTF-8' },
cdata:true,
});

var xml = builder.buildObject(json);
response.write(json);
/*console.log(util.inspect(builder, false, null));*/
});

response.end();
}
catch(e){
console.log(e);
}
});


console.log("Server running at port "+PORT);
try{
server.listen(PORT);
}
catch(e){
console.log(e);
}









share|improve this question

























  • Both JSON and XML should have a root element (unless the JSON is an array that is, but then you should not try to convert it to XML). It would be wise to provide an example JSON and the resulting XML in the question, just to illustrate it better.

    – grochmal
    Jul 7 '16 at 11:43











  • Wait, you're making the JSON->XML using xml2js.Builder() not xml2js.Parser(), right?

    – grochmal
    Jul 7 '16 at 12:24











  • @grochmal right.

    – AngularLearner
    Jul 11 '16 at 7:00
















3















I'm trying creating a XML from JSON obj and its giving me root element in the result, I tried setting the explicitRoot var parser = xml2js.Parser({explicitRoot:false});
to false but it does not remove the default root tag but just removing my orignal XML root tag (<VSAT></VSAT>)



Processing XML using xml2js



<?xml version="1.0" encoding="utf-8"?>
<VAST version="2.0">
<Ad id="72419"></Ad>
</VAST>


Resulting XML:



<?xml version="1.0" encoding="utf-8"?>
<root>
<VAST version="2.0">
<Ad id="72419"></Ad>
</VAST>
</root>


Any idea ?



full code



/*
NodeJS server
*/
var http = require('http');
var xml2js = require('xml2js');
var fs = require('fs');
var util = require('util');
var json,PORT=2000;

var server = http.createServer(function(request, response){
response.writeHead(200,{'Content-Type':'text/html'});
try{
var filedata = fs.readFileSync('vast_all.xml','ascii');

var parser = xml2js.Parser({explicitRoot:true});
parser.parseString(filedata.substring(0,filedata.length),function(err,result){
result.new = 'test';
json = JSON.stringify(result);

var builder = new xml2js.Builder({
xmldec:{ 'version': '1.0', 'encoding': 'UTF-8' },
cdata:true,
});

var xml = builder.buildObject(json);
response.write(json);
/*console.log(util.inspect(builder, false, null));*/
});

response.end();
}
catch(e){
console.log(e);
}
});


console.log("Server running at port "+PORT);
try{
server.listen(PORT);
}
catch(e){
console.log(e);
}









share|improve this question

























  • Both JSON and XML should have a root element (unless the JSON is an array that is, but then you should not try to convert it to XML). It would be wise to provide an example JSON and the resulting XML in the question, just to illustrate it better.

    – grochmal
    Jul 7 '16 at 11:43











  • Wait, you're making the JSON->XML using xml2js.Builder() not xml2js.Parser(), right?

    – grochmal
    Jul 7 '16 at 12:24











  • @grochmal right.

    – AngularLearner
    Jul 11 '16 at 7:00














3












3








3








I'm trying creating a XML from JSON obj and its giving me root element in the result, I tried setting the explicitRoot var parser = xml2js.Parser({explicitRoot:false});
to false but it does not remove the default root tag but just removing my orignal XML root tag (<VSAT></VSAT>)



Processing XML using xml2js



<?xml version="1.0" encoding="utf-8"?>
<VAST version="2.0">
<Ad id="72419"></Ad>
</VAST>


Resulting XML:



<?xml version="1.0" encoding="utf-8"?>
<root>
<VAST version="2.0">
<Ad id="72419"></Ad>
</VAST>
</root>


Any idea ?



full code



/*
NodeJS server
*/
var http = require('http');
var xml2js = require('xml2js');
var fs = require('fs');
var util = require('util');
var json,PORT=2000;

var server = http.createServer(function(request, response){
response.writeHead(200,{'Content-Type':'text/html'});
try{
var filedata = fs.readFileSync('vast_all.xml','ascii');

var parser = xml2js.Parser({explicitRoot:true});
parser.parseString(filedata.substring(0,filedata.length),function(err,result){
result.new = 'test';
json = JSON.stringify(result);

var builder = new xml2js.Builder({
xmldec:{ 'version': '1.0', 'encoding': 'UTF-8' },
cdata:true,
});

var xml = builder.buildObject(json);
response.write(json);
/*console.log(util.inspect(builder, false, null));*/
});

response.end();
}
catch(e){
console.log(e);
}
});


console.log("Server running at port "+PORT);
try{
server.listen(PORT);
}
catch(e){
console.log(e);
}









share|improve this question
















I'm trying creating a XML from JSON obj and its giving me root element in the result, I tried setting the explicitRoot var parser = xml2js.Parser({explicitRoot:false});
to false but it does not remove the default root tag but just removing my orignal XML root tag (<VSAT></VSAT>)



Processing XML using xml2js



<?xml version="1.0" encoding="utf-8"?>
<VAST version="2.0">
<Ad id="72419"></Ad>
</VAST>


Resulting XML:



<?xml version="1.0" encoding="utf-8"?>
<root>
<VAST version="2.0">
<Ad id="72419"></Ad>
</VAST>
</root>


Any idea ?



full code



/*
NodeJS server
*/
var http = require('http');
var xml2js = require('xml2js');
var fs = require('fs');
var util = require('util');
var json,PORT=2000;

var server = http.createServer(function(request, response){
response.writeHead(200,{'Content-Type':'text/html'});
try{
var filedata = fs.readFileSync('vast_all.xml','ascii');

var parser = xml2js.Parser({explicitRoot:true});
parser.parseString(filedata.substring(0,filedata.length),function(err,result){
result.new = 'test';
json = JSON.stringify(result);

var builder = new xml2js.Builder({
xmldec:{ 'version': '1.0', 'encoding': 'UTF-8' },
cdata:true,
});

var xml = builder.buildObject(json);
response.write(json);
/*console.log(util.inspect(builder, false, null));*/
});

response.end();
}
catch(e){
console.log(e);
}
});


console.log("Server running at port "+PORT);
try{
server.listen(PORT);
}
catch(e){
console.log(e);
}






xml node.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 11 '16 at 7:19







AngularLearner

















asked Jul 7 '16 at 11:40









AngularLearnerAngularLearner

3910




3910













  • Both JSON and XML should have a root element (unless the JSON is an array that is, but then you should not try to convert it to XML). It would be wise to provide an example JSON and the resulting XML in the question, just to illustrate it better.

    – grochmal
    Jul 7 '16 at 11:43











  • Wait, you're making the JSON->XML using xml2js.Builder() not xml2js.Parser(), right?

    – grochmal
    Jul 7 '16 at 12:24











  • @grochmal right.

    – AngularLearner
    Jul 11 '16 at 7:00



















  • Both JSON and XML should have a root element (unless the JSON is an array that is, but then you should not try to convert it to XML). It would be wise to provide an example JSON and the resulting XML in the question, just to illustrate it better.

    – grochmal
    Jul 7 '16 at 11:43











  • Wait, you're making the JSON->XML using xml2js.Builder() not xml2js.Parser(), right?

    – grochmal
    Jul 7 '16 at 12:24











  • @grochmal right.

    – AngularLearner
    Jul 11 '16 at 7:00

















Both JSON and XML should have a root element (unless the JSON is an array that is, but then you should not try to convert it to XML). It would be wise to provide an example JSON and the resulting XML in the question, just to illustrate it better.

– grochmal
Jul 7 '16 at 11:43





Both JSON and XML should have a root element (unless the JSON is an array that is, but then you should not try to convert it to XML). It would be wise to provide an example JSON and the resulting XML in the question, just to illustrate it better.

– grochmal
Jul 7 '16 at 11:43













Wait, you're making the JSON->XML using xml2js.Builder() not xml2js.Parser(), right?

– grochmal
Jul 7 '16 at 12:24





Wait, you're making the JSON->XML using xml2js.Builder() not xml2js.Parser(), right?

– grochmal
Jul 7 '16 at 12:24













@grochmal right.

– AngularLearner
Jul 11 '16 at 7:00





@grochmal right.

– AngularLearner
Jul 11 '16 at 7:00












3 Answers
3






active

oldest

votes


















2














Set the headless to true when instantiating Builder e.g.:



let builder = new xml2js.Builder({headless: true});



This worked for me. It removed the:



<?xml version="1.0" encoding="UTF-8" standalone="yes"?>



But this is only supported from version 0.4.3






share|improve this answer































    1














    This is too long for a comment.



    The correct way to create XML from JSON with xml2js is using xml2js.Builder. For example, in the following snippet the JSON is changed into an XML string and then parsed back to JSON:



    var fs = require('fs');
    var xml2js = require('xml2js');

    var data = { VAST: { '$': { version: '2.0' }
    , Ad: { '$': { id: '72419' }, content: "yay" } } };
    console.log("the JSON");
    console.log(JSON.stringify(data));

    // the JSON
    // {"VAST":{"$":{"version":"2.0"},"Ad":{"$":{"id":"72419"},"content":"yay"}}}

    var builder = new xml2js.Builder();
    var xml = builder.buildObject(data);
    console.log("result from xml2js.builder");
    console.log(xml); // this is a string

    // result from xml2js.builder
    // <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    // <VAST version="2.0">
    // <Ad id="72419">
    // <content>yay</content>
    // </Ad>
    // </VAST>

    var parser = new xml2js.Parser();
    parser.parseString(xml, function(err, result) {
    console.log("result of parsing it back");
    console.log(JSON.stringify(result));
    console.log(result.VAST['Ad'][0]['$']['id']); // you can get the id back
    });

    // result of parsing it back
    // {"VAST":{"$":{"version":"2.0"},"Ad":[{"$":{"id":"72419"},"content":["yay"]}]}}
    // 72419


    The parser adds an extra array ("Ad":[{"$") because you can have more than one Ad tag (and this allows for a clean way to access things in node later).





    Now, if you add this:



    var badxml = builder.buildObject(xml);
    console.log(badxml);


    To the end of that snippet then you do get an XML with an extra root element:



    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <root>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
    &lt;VAST version="2.0"&gt;
    &lt;Ad id="72419"&gt;
    &lt;content&gt;yay&lt;/content&gt;
    &lt;/Ad&gt;
    &lt;/VAST&gt;</root>


    But it is still quite different from what you are getting, since it is properly escaped.






    share|improve this answer
























    • I'm manipulating XML with xml2js and adding new nodes and then converting back it to XML (from JSON)

      – AngularLearner
      Jul 11 '16 at 7:05



















    0














    you can't remove root node but you can change its name like this:



     let builder = new xml2js.Builder({headless: true , explicitRoot : false , rootName :'Search_Query'});
    let xml = builder.buildObject(req.param('Search_Query'));


    This will change the root to Search_Query






    share|improve this answer























      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',
      autoActivateHeartbeat: false,
      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%2f38244545%2fnode-js-remove-root-node-from-resulting-xml-using-xml2js%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      Set the headless to true when instantiating Builder e.g.:



      let builder = new xml2js.Builder({headless: true});



      This worked for me. It removed the:



      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>



      But this is only supported from version 0.4.3






      share|improve this answer




























        2














        Set the headless to true when instantiating Builder e.g.:



        let builder = new xml2js.Builder({headless: true});



        This worked for me. It removed the:



        <?xml version="1.0" encoding="UTF-8" standalone="yes"?>



        But this is only supported from version 0.4.3






        share|improve this answer


























          2












          2








          2







          Set the headless to true when instantiating Builder e.g.:



          let builder = new xml2js.Builder({headless: true});



          This worked for me. It removed the:



          <?xml version="1.0" encoding="UTF-8" standalone="yes"?>



          But this is only supported from version 0.4.3






          share|improve this answer













          Set the headless to true when instantiating Builder e.g.:



          let builder = new xml2js.Builder({headless: true});



          This worked for me. It removed the:



          <?xml version="1.0" encoding="UTF-8" standalone="yes"?>



          But this is only supported from version 0.4.3







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Sep 5 '17 at 12:20









          Ivan PandžićIvan Pandžić

          177112




          177112

























              1














              This is too long for a comment.



              The correct way to create XML from JSON with xml2js is using xml2js.Builder. For example, in the following snippet the JSON is changed into an XML string and then parsed back to JSON:



              var fs = require('fs');
              var xml2js = require('xml2js');

              var data = { VAST: { '$': { version: '2.0' }
              , Ad: { '$': { id: '72419' }, content: "yay" } } };
              console.log("the JSON");
              console.log(JSON.stringify(data));

              // the JSON
              // {"VAST":{"$":{"version":"2.0"},"Ad":{"$":{"id":"72419"},"content":"yay"}}}

              var builder = new xml2js.Builder();
              var xml = builder.buildObject(data);
              console.log("result from xml2js.builder");
              console.log(xml); // this is a string

              // result from xml2js.builder
              // <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
              // <VAST version="2.0">
              // <Ad id="72419">
              // <content>yay</content>
              // </Ad>
              // </VAST>

              var parser = new xml2js.Parser();
              parser.parseString(xml, function(err, result) {
              console.log("result of parsing it back");
              console.log(JSON.stringify(result));
              console.log(result.VAST['Ad'][0]['$']['id']); // you can get the id back
              });

              // result of parsing it back
              // {"VAST":{"$":{"version":"2.0"},"Ad":[{"$":{"id":"72419"},"content":["yay"]}]}}
              // 72419


              The parser adds an extra array ("Ad":[{"$") because you can have more than one Ad tag (and this allows for a clean way to access things in node later).





              Now, if you add this:



              var badxml = builder.buildObject(xml);
              console.log(badxml);


              To the end of that snippet then you do get an XML with an extra root element:



              <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
              <root>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
              &lt;VAST version="2.0"&gt;
              &lt;Ad id="72419"&gt;
              &lt;content&gt;yay&lt;/content&gt;
              &lt;/Ad&gt;
              &lt;/VAST&gt;</root>


              But it is still quite different from what you are getting, since it is properly escaped.






              share|improve this answer
























              • I'm manipulating XML with xml2js and adding new nodes and then converting back it to XML (from JSON)

                – AngularLearner
                Jul 11 '16 at 7:05
















              1














              This is too long for a comment.



              The correct way to create XML from JSON with xml2js is using xml2js.Builder. For example, in the following snippet the JSON is changed into an XML string and then parsed back to JSON:



              var fs = require('fs');
              var xml2js = require('xml2js');

              var data = { VAST: { '$': { version: '2.0' }
              , Ad: { '$': { id: '72419' }, content: "yay" } } };
              console.log("the JSON");
              console.log(JSON.stringify(data));

              // the JSON
              // {"VAST":{"$":{"version":"2.0"},"Ad":{"$":{"id":"72419"},"content":"yay"}}}

              var builder = new xml2js.Builder();
              var xml = builder.buildObject(data);
              console.log("result from xml2js.builder");
              console.log(xml); // this is a string

              // result from xml2js.builder
              // <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
              // <VAST version="2.0">
              // <Ad id="72419">
              // <content>yay</content>
              // </Ad>
              // </VAST>

              var parser = new xml2js.Parser();
              parser.parseString(xml, function(err, result) {
              console.log("result of parsing it back");
              console.log(JSON.stringify(result));
              console.log(result.VAST['Ad'][0]['$']['id']); // you can get the id back
              });

              // result of parsing it back
              // {"VAST":{"$":{"version":"2.0"},"Ad":[{"$":{"id":"72419"},"content":["yay"]}]}}
              // 72419


              The parser adds an extra array ("Ad":[{"$") because you can have more than one Ad tag (and this allows for a clean way to access things in node later).





              Now, if you add this:



              var badxml = builder.buildObject(xml);
              console.log(badxml);


              To the end of that snippet then you do get an XML with an extra root element:



              <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
              <root>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
              &lt;VAST version="2.0"&gt;
              &lt;Ad id="72419"&gt;
              &lt;content&gt;yay&lt;/content&gt;
              &lt;/Ad&gt;
              &lt;/VAST&gt;</root>


              But it is still quite different from what you are getting, since it is properly escaped.






              share|improve this answer
























              • I'm manipulating XML with xml2js and adding new nodes and then converting back it to XML (from JSON)

                – AngularLearner
                Jul 11 '16 at 7:05














              1












              1








              1







              This is too long for a comment.



              The correct way to create XML from JSON with xml2js is using xml2js.Builder. For example, in the following snippet the JSON is changed into an XML string and then parsed back to JSON:



              var fs = require('fs');
              var xml2js = require('xml2js');

              var data = { VAST: { '$': { version: '2.0' }
              , Ad: { '$': { id: '72419' }, content: "yay" } } };
              console.log("the JSON");
              console.log(JSON.stringify(data));

              // the JSON
              // {"VAST":{"$":{"version":"2.0"},"Ad":{"$":{"id":"72419"},"content":"yay"}}}

              var builder = new xml2js.Builder();
              var xml = builder.buildObject(data);
              console.log("result from xml2js.builder");
              console.log(xml); // this is a string

              // result from xml2js.builder
              // <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
              // <VAST version="2.0">
              // <Ad id="72419">
              // <content>yay</content>
              // </Ad>
              // </VAST>

              var parser = new xml2js.Parser();
              parser.parseString(xml, function(err, result) {
              console.log("result of parsing it back");
              console.log(JSON.stringify(result));
              console.log(result.VAST['Ad'][0]['$']['id']); // you can get the id back
              });

              // result of parsing it back
              // {"VAST":{"$":{"version":"2.0"},"Ad":[{"$":{"id":"72419"},"content":["yay"]}]}}
              // 72419


              The parser adds an extra array ("Ad":[{"$") because you can have more than one Ad tag (and this allows for a clean way to access things in node later).





              Now, if you add this:



              var badxml = builder.buildObject(xml);
              console.log(badxml);


              To the end of that snippet then you do get an XML with an extra root element:



              <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
              <root>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
              &lt;VAST version="2.0"&gt;
              &lt;Ad id="72419"&gt;
              &lt;content&gt;yay&lt;/content&gt;
              &lt;/Ad&gt;
              &lt;/VAST&gt;</root>


              But it is still quite different from what you are getting, since it is properly escaped.






              share|improve this answer













              This is too long for a comment.



              The correct way to create XML from JSON with xml2js is using xml2js.Builder. For example, in the following snippet the JSON is changed into an XML string and then parsed back to JSON:



              var fs = require('fs');
              var xml2js = require('xml2js');

              var data = { VAST: { '$': { version: '2.0' }
              , Ad: { '$': { id: '72419' }, content: "yay" } } };
              console.log("the JSON");
              console.log(JSON.stringify(data));

              // the JSON
              // {"VAST":{"$":{"version":"2.0"},"Ad":{"$":{"id":"72419"},"content":"yay"}}}

              var builder = new xml2js.Builder();
              var xml = builder.buildObject(data);
              console.log("result from xml2js.builder");
              console.log(xml); // this is a string

              // result from xml2js.builder
              // <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
              // <VAST version="2.0">
              // <Ad id="72419">
              // <content>yay</content>
              // </Ad>
              // </VAST>

              var parser = new xml2js.Parser();
              parser.parseString(xml, function(err, result) {
              console.log("result of parsing it back");
              console.log(JSON.stringify(result));
              console.log(result.VAST['Ad'][0]['$']['id']); // you can get the id back
              });

              // result of parsing it back
              // {"VAST":{"$":{"version":"2.0"},"Ad":[{"$":{"id":"72419"},"content":["yay"]}]}}
              // 72419


              The parser adds an extra array ("Ad":[{"$") because you can have more than one Ad tag (and this allows for a clean way to access things in node later).





              Now, if you add this:



              var badxml = builder.buildObject(xml);
              console.log(badxml);


              To the end of that snippet then you do get an XML with an extra root element:



              <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
              <root>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
              &lt;VAST version="2.0"&gt;
              &lt;Ad id="72419"&gt;
              &lt;content&gt;yay&lt;/content&gt;
              &lt;/Ad&gt;
              &lt;/VAST&gt;</root>


              But it is still quite different from what you are getting, since it is properly escaped.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jul 7 '16 at 13:03









              grochmalgrochmal

              1,85021222




              1,85021222













              • I'm manipulating XML with xml2js and adding new nodes and then converting back it to XML (from JSON)

                – AngularLearner
                Jul 11 '16 at 7:05



















              • I'm manipulating XML with xml2js and adding new nodes and then converting back it to XML (from JSON)

                – AngularLearner
                Jul 11 '16 at 7:05

















              I'm manipulating XML with xml2js and adding new nodes and then converting back it to XML (from JSON)

              – AngularLearner
              Jul 11 '16 at 7:05





              I'm manipulating XML with xml2js and adding new nodes and then converting back it to XML (from JSON)

              – AngularLearner
              Jul 11 '16 at 7:05











              0














              you can't remove root node but you can change its name like this:



               let builder = new xml2js.Builder({headless: true , explicitRoot : false , rootName :'Search_Query'});
              let xml = builder.buildObject(req.param('Search_Query'));


              This will change the root to Search_Query






              share|improve this answer




























                0














                you can't remove root node but you can change its name like this:



                 let builder = new xml2js.Builder({headless: true , explicitRoot : false , rootName :'Search_Query'});
                let xml = builder.buildObject(req.param('Search_Query'));


                This will change the root to Search_Query






                share|improve this answer


























                  0












                  0








                  0







                  you can't remove root node but you can change its name like this:



                   let builder = new xml2js.Builder({headless: true , explicitRoot : false , rootName :'Search_Query'});
                  let xml = builder.buildObject(req.param('Search_Query'));


                  This will change the root to Search_Query






                  share|improve this answer













                  you can't remove root node but you can change its name like this:



                   let builder = new xml2js.Builder({headless: true , explicitRoot : false , rootName :'Search_Query'});
                  let xml = builder.buildObject(req.param('Search_Query'));


                  This will change the root to Search_Query







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 1 at 6:45









                  Salahudin MalikSalahudin Malik

                  8012




                  8012






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38244545%2fnode-js-remove-root-node-from-resulting-xml-using-xml2js%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas