title: Deep level xml parsing to csv using NodeJS
body: <p>I have a medium sized xml ~ 5mb that needs to be converted to csv. </p>
  
  <p>Obviously wont go for reinventing the wheel, 
  so a two layer approach - 
  1> xml to json 
  2> json to csv</p>
  
  <p>My current code is : </p>
  
  <pre><code>const xml_obj = {}
  const htt = require('http-status-code-node');
  var fs = require('fs'); 
  var xml2js = require('xml2js');
  var converter = require('json-2-csv'); 
  
  xml_obj["convert"] = (req, res, next) =&gt; { 
    var parser = new xml2js.Parser();
    fs.readFile(__dirname + '/directoryexport.xml', function (err, data) {   
      parser.parseString(data, function (err, result) { 
        console.log('Done'); 
        var callback = function (err, ycsv) {
          if (err) return console.log(err); 
          ///
          res.setHeader('Content-Disposition', 'attachment; filename=testing.csv');
          res.set('Content-Type', 'text/csv');
          res.status(200).send(result);
          ///
        }
        var documents = [];      
        documents.push(result)
        converter.json2csv(documents, callback);      
      })
    });
  } 
  module.exports = xml_obj.convert
  </code></pre>
  
  <p>However the xml being nested gives a multi layered json which the yields a single string instead of a proper delimited csv.. </p>
  
  <p><a href="https://drive.google.com/open?id=0B8u8XADz9kHeTjFZaEtQMm9vcE0" rel="nofollow noreferrer">The current output CSV</a></p>
  
  <p><a href="https://drive.google.com/open?id=0B8u8XADz9kHebWxfMnF3d1FyVFE" rel="nofollow noreferrer">The Original xml</a></p>
  
  <p><a href="https://gist.github.com/crazysal/df23405476d1da26c1ac486cb0815ba0" rel="nofollow noreferrer">The XML structure</a></p>
  
  <p><a href="https://gist.github.com/crazysal/82caabc1177ae84ffe5902e88ff02f61" rel="nofollow noreferrer">The Json I get on converting xml</a></p>
  
  <p>Also as per the documentation of the json to csv converter 
  if the input json is in a proper structure like : </p>
  
  <pre><code>[
      {
          Make: 'Nissan',
          Model: 'Murano',
          Year: '2013',
          Specifications: {
              Mileage: '7106',
              Trim: 'S AWD'
          }
      },
      {
          Make: 'BMW',
          Model: 'X5',
          Year: '2014',
          Specifications: {
              Mileage: '3287',
              Trim: 'M'
          }
      }
  ];
  </code></pre>
  
  <p>This yields a very nicely formatted csv like this : <a href="https://drive.google.com/open?id=0B8u8XADz9kHeTjFZaEtQMm9vcE0" rel="nofollow noreferrer">Example Perfect CSV From JSON</a></p>
  
  <p>Edit 1 :
  The format I'm looking for is somewhat like, 
  It's important to capture all parent organization and organizationalUnit details for each person node.
  For example, </p>
  
  <blockquote>
    <p>organizationalUnit UUID "b3b05b77-a8a7-43ed-ab74-b7d898c60296" should
    produce a CSV lines like:</p>
  </blockquote>
  
  <pre><code>"Mr Shayne Howard","Howard","Shayne","Mr","","Branch Manager","(02) 6121 5492","","Level 1, 12 Mort Street, Canberra, ACT, 2601","Shayne.Howard@employment.gov.au","","b43e0864-1b9a-40f0-8049-c90af5f9141c","","GPO Box 9880 CANBERRA ACT 2601 Australia",1392,"","Department of Employment","","1300 488 064","","","","http://www.employment.gov.au","GPO Box 9880, Canberra ACT 2601","EMPLOYMENT"
  "Mr Luke de Jong","De Jong","Luke","Mr","","Branch Manager, General Counsel","(02) 6240 0909",""(02) 6123 5100"","","Luke.deJong@employment.gov.au","","58a503a8-ce8b-41c0-b690-b9f9efd98a89","","GPO Box 9880 CANBERRA ACT 2601",1393,"","Department of Employment","","1300 488 064","","","","http://www.employment.gov.au","GPO Box 9880, Canberra ACT 2601","EMPLOYMENT"
  </code></pre>
  
  <p>Edit 2 : 
  Flattening out the json is a good idea, but its not capturing the entire data.
  Using the <strong>camaro nodejs module</strong>  with the following template : </p>
  
  <pre><code>persons: ['//person', {
        root_organization_name: '../../../../name',
        main_organization_name: '../../../name',
        main_organization_website: '../../../website',
        fullName: 'fullName',
        familyName: 'familyName',
        firstName: 'firstName',
        personalTitle: 'personalTitle',
        title: 'title',
        person_phone: 'phone',
        person_location: 'location',
        person_fax: 'fax',
        otherRolesDN: 'otherRolesDN',
        person_mail: 'mail',
        informationPublicationScheme: '../informationPublicationScheme',
        publications: '../../publications',
        annualReport: '../../annualReport',
        mediaReleases: '../../mediaReleases',
        organizationUnit_1_name: '../../name',
        organizationUnit_1_description: '../../description',
        organizationUnit_1_location: '../../location',
        organizationUnit_1_phone: '../../phone',
        organizationUnit_1_fax: '../../fax',
        organizationUnit_1_website: '../../website',
        organizationUnit_2_name: '../name',
        organizationUnit_2_location: '../location',
        organizationUnit_2_phone: '../phone',
        organizationUnit_2_fax: '../fax',
        organizationUnit_2_website: '../website',
        occupantName: './role/occupantName',
        roleName: './role/roleName',
        occupantUUID: './role/occupantUUID',
        role_phone: './role/phone',
        role_fax: './role/fax',
        role_location: './role/location',
        role_mail: './role/ mail'
      }]
  </code></pre>
  
  <p>How could I also get the roles array. 
  Also current csv gets some rows of data in wrong columns : </p>
  
  <p><a href="https://drive.google.com/open?id=0B8u8XADz9kHeOTUwT1RnVUVkR28" rel="nofollow noreferrer">Wrong csv after camaro </a>
  Any tips on how to make this work with my input. </p>