title: How to add/modify a node in XML file using node js
body: <p>Let's say I have the following xml code</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;catalog&gt;
    &lt;person&gt;
      &lt;name&gt;John&lt;/name&gt;
      &lt;surname&gt;Smith&lt;/surname&gt;
    &lt;/person&gt;
    &lt;person&gt;
      &lt;name&gt;Abe&lt;/name&gt;
      &lt;surname&gt;Lincoln&lt;/surname&gt;
    &lt;/person&gt;
    &lt;person&gt;
      &lt;name&gt;James&lt;/name&gt;
      &lt;surname&gt;Bond&lt;/surname&gt;
    &lt;/person&gt;
  &lt;/catalog&gt;
  </code></pre>
  
  <p>And I want to add a new node, let's say the following:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;person&gt;
    &lt;name&gt;Tony&lt;/name&gt;
    &lt;surname&gt;Stark&lt;/surname&gt;
  &lt;/person&gt;
  </code></pre>
  
  <p>How do I do this in node js? I mean I have a file (/routes/index.js in node js express, to be exact), and I want to be able to add/modify existing xml file. I've tried <strong>fs.writeFile()</strong>, but this writes a whole new file, and <strong>fs.appendFile()</strong> adds a header(?xml + encoding and stuff) and root nodes after the last node, so I can't insert it into the catalog node. I can't get rid of the xml declaration header either.<br>
  I've been using .builder() to do this, and it looked like this  </p>
  
  <pre><code>router.post('/addnode', function (req, res) {
  
    var obj = {name: "Tony", surname: "Stark"};
    var fs = require('fs');
    var xml2js = require('xml2js');
  
    var builder = new xml2js.Builder();
    var xml = builder.buildObject(obj);  
  
    fs.writeFile('public/test.xml', xml, function (err) {
      if (err) throw err;
      console.log('It\'s saved!');
    }); 
  });
  </code></pre>
  
  <p>Finally I want to get that data from a from <strong>addnode</strong>, so I won't create a <strong>var obj</strong> in this function.</p>