title: Passing XML File To xml2js With Node.js
body: <p><strong>Background</strong></p>
  
  <p>I have taken some sample data from a large <code>XML</code> response I will be working with in my app. I decided to save the xml to a <code>response.xml</code> file in my app so I can parse the data without making a bunch of unnecessary request to the api. </p>
  
  <p>I am going to be using <code>xml2js</code> to convert the xml response to a <code>JS</code> <code>Object</code>. </p>
  
  <p><strong>Problem</strong></p>
  
  <p>I have been able to open the file and <code>console.log()</code> it. </p>
  
  <p>I have been able to run <code>xml2js</code> by passing a small xml string to it which I also <code>console.log()</code> it. </p>
  
  <p>But I have only been able to <code>console.log()</code> the file after xml2js creates the object. No matter what I try I continue to get null when using return or passing trying to pass the data outside of the initial creation of the object. </p>
  
  <p><strong>Example</strong></p>
  
  <p>This prints <code>xml tree</code> to the console,</p>
  
  <pre><code>var fs = require('fs');
  var openFile = fs.readFile('./response.xml', 'utf8', function (err,data) {
      if (err) {
          return console.log(err);
      }
      console.log(data);
  });
  
  function requestCreditReport() {
      return openFile;
  }
  
  requestCreditReport();
  </code></pre>
  
  <p>This prints a small <code>xml string</code> to console statically added in with 
  <code>xml2js</code>,</p>
  
  <pre><code>var parseString = require('xml2js').parseString;
  
  var xml = "&lt;root&gt;Hello xml2js!&lt;/root&gt;";
  
  parseString(xml, function (err, result) {
      console.dir(result);
  });
  </code></pre>
  
  <p><strong>Question</strong></p>
  
  <p>How do I use the object once it is created outside of the method below. Initially when it is created I can output it to the console but cannot seem to access it outside of that <code>console.log()</code>. in the example below I am trying <code>return result</code>. This leaves the value null when I try to pass the object returned by the function to a variable like this,</p>
  
  <pre><code>var response = requestReport();
  </code></pre>
  
  <p>Recent try,</p>
  
  <pre><code>var fs = require('fs');
  var parseString = require('xml2js').parseString;
  
  function requestReport() {
      fs.readFile('./response.xml', 'utf8', function (err,data) {
          if (err) {
              return console.log(err);
          }
          return parseString(data, function (err, result) {
              return result;
          });
      });
  }
  
  var response = requestReport();
  
  console.log(response);
  </code></pre>
  
  <p>This returns <code>null</code>. But if I console.log(result) instead or using return then trying outside of the function it returns this,</p>
  
  <pre><code>{ RESPONSE_DATA:
     { '$': { MYAPI: '0.0.0' },
       DETAILS: [ [Object] ],
       DETAILS_ACCOUNT: [ [Object] ],
       RESPONSE: [ [Object] ] } }
  </code></pre>