title: Node + xmldom: How do I change the value of a single XML field in javascript?
body: <p>Using <a href="https://nodejs.org/en/" rel="noreferrer">node</a> v.0.10.29, <a href="http://expressjs.com/" rel="noreferrer">Express</a> v4.12.0, and <a href="https://www.npmjs.com/package/xmldom" rel="noreferrer">xmldom</a> v0.1.19, I'm trying to do the following:</p>
  
  <h1>Steps</h1>
  
  <ol>
  <li>Read an <strong>XML file into a string</strong></li>
  <li><strong>Convert the string</strong> into an <strong>XML object</strong> using <a href="https://www.npmjs.com/package/xmldom" rel="noreferrer">xmldom</a></li>
  <li>Set the <code>&lt;name&gt;default&lt;/name&gt;</code> field to <code>&lt;name&gt;test&lt;/name&gt;</code></li>
  <li><strong>Convert the XML object back into a string</strong></li>
  </ol>
  
  <h1>Problem</h1>
  
  <p><em>The problem is that after I set the <code>&lt;name&gt;</code> field, it sets correctly in the object, but when I convert it to a string, the <code>&lt;name&gt;</code> field is back to being the old value (wrong).</em> </p>
  
  <h1>Code</h1>
  
  <p>Here's what the code looks like for this:</p>
  
  <pre><code>var fs = require('fs');
  var DOMParser = require('xmldom').DOMParser;
  var XMLSerializer = require('xmldom').XMLSerializer;
  var filename = "myFile.xml";
  
  fs.readFile(filename, "utf-8", function (err,data) 
  {
      //CREATE/PARSE XML OBJECT FROM STRING
      var customerConfig = new DOMParser().parseFromString(data);
  
      //SET VALUE TO "TEST" (&lt;name&gt;default&lt;/name&gt; TO &lt;name&gt;test&lt;/name&gt;)
      customerConfig.getElementsByTagName("name")[0].childNodes[0].nodeValue = "test";
  
      //THIS OUTPUTS "test" WHICH IS CORRECT - 
      console.log(customerConfig.getElementsByTagName("name")[0].childNodes[0].nodeValue);
  
      //SERIALIZE TO STRING
      var xmlString = new XMLSerializer().serializeToString(customerConfig);
  
      //THIS OUTPUTS THE FULL XML FILE, 
      //BUT STILL SHOWS &lt;name&gt;default&lt;/name&gt; AND NOT &lt;name&gt;test&lt;/name&gt;
      console.log(xmlString);
  });
  </code></pre>
  
  <p><strong>The problem is that the <code>&lt;name&gt;</code> field isn't setting to <code>test</code></strong> in the string... I'm thinking there is a <strong>problem with the serialization part?</strong> Anyone see what I'm doing wrong?</p>
  
  <p>Thank you!!</p>