Json 使用NodeJS对csv进行深层xml解析

Json 使用NodeJS对csv进行深层xml解析,json,node.js,xml,csv,Json,Node.js,Xml,Csv,我有一个中等大小的xml~5mb,需要转换为csv 显然,他不会去重新发明轮子, 因此,两层方法- 1> xml到json 2> json到csv 我目前的代码是: 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_

我有一个中等大小的xml~5mb,需要转换为csv

显然,他不会去重新发明轮子, 因此,两层方法- 1> xml到json 2> json到csv

我目前的代码是:

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) => { 
  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
但是,嵌套的xml提供了一个多层json,它生成一个字符串,而不是一个正确分隔的csv

同样根据json到csv转换器的文档 如果输入json的结构正确,如:

[
    {
        Make: 'Nissan',
        Model: 'Murano',
        Year: '2013',
        Specifications: {
            Mileage: '7106',
            Trim: 'S AWD'
        }
    },
    {
        Make: 'BMW',
        Model: 'X5',
        Year: '2014',
        Specifications: {
            Mileage: '3287',
            Trim: 'M'
        }
    }
];
这将生成一个格式非常好的csv,如下所示:

编辑1: 我要找的格式有点像, 捕获每个人员节点的所有父组织和organizationalUnit详细信息非常重要。 比如说,

组织单位UUID“b3b05b77-a8a7-43ed-ab74-b7d898c60296”应 生成CSV行,如:

编辑2: 平展json是一个好主意,但它不能捕获整个数据。 使用带有以下模板的camaro nodejs模块:

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'
    }]
我怎样才能得到角色数组呢。 此外,当前csv在错误的列中获取了一些数据行:


关于如何使用我的输入进行此操作的任何提示

由于json结构的输出很深,如果您希望它正确地转换为csv,您必须将其展平

似乎你只对最深层次感兴趣。下面是一个示例,如果您想添加更多数据,可以随意添加到模板中

const transform = require('camaro')
const tocsv = require('json2csv')
const fs = require('fs')

const xml = fs.readFileSync('so.xml', 'utf-8')
const template = {
    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',
        roles: ['../role', {
            occupantName: 'occupantName',
            roleName: 'roleName',
            occupantUUID: 'occupantUUID',
            role_phone: 'phone',
            role_fax: 'fax',
            role_location: 'location',
            role_mail: ' mail'
        }]
    }]
}

const result = transform(xml, template)
console.log(JSON.stringify(result.roles, null, 4))
输出json的示例(如果需要,可以使用json2csv转换为csv)


由于json结构的输出很深,如果您想将其正确转换为csv,就必须将其展平

似乎你只对最深层次感兴趣。下面是一个示例,如果您想添加更多数据,可以随意添加到模板中

const transform = require('camaro')
const tocsv = require('json2csv')
const fs = require('fs')

const xml = fs.readFileSync('so.xml', 'utf-8')
const template = {
    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',
        roles: ['../role', {
            occupantName: 'occupantName',
            roleName: 'roleName',
            occupantUUID: 'occupantUUID',
            role_phone: 'phone',
            role_fax: 'fax',
            role_location: 'location',
            role_mail: ' mail'
        }]
    }]
}

const result = transform(xml, template)
console.log(JSON.stringify(result.roles, null, 4))
输出json的示例(如果需要,可以使用json2csv转换为csv)


你能展示你想要什么样的输出格式吗?用同样的@tuananhtry更新问题,看看下面的代码是否有用。你能展示你想要什么样的输出格式吗?用同样的@tuananhtry更新问题,看看下面的代码是否有用。这很好,但这只是给出json中最深的节点,你能调整它使xml的所有层都变成一个平面结构吗?在我的示例代码中,我给出了一个如何获取上层节点数据的示例。基本上,您可以使用
。/
提升1级。像这样
organizationUnit:'../name'
如果您看到电话号码中的数据正在泄漏到姓名列中。而这样的缩进错误请勾选突出显示fields@SaleemAhmed请参阅更新的答案和角色。我看到角色与person?处于同一级别@salemahmed我在我的输出中没有看到类似的内容。你能检查一下我在这里得到的输出吗?这很好,但这只是给出了json最深的节点,你能调整它使xml的所有层都变成一个平面结构吗?在我的示例代码中,我给出了一个如何获取上层节点数据的示例。基本上,您可以使用
。/
提升1级。像这样
organizationUnit:'../name'
如果您看到电话号码中的数据正在泄漏到姓名列中。而这样的缩进错误请勾选突出显示fields@SaleemAhmed请参阅更新的答案和角色。我看到角色与person?处于同一级别@salemahmed我在我的输出中没有看到类似的内容。你能检查一下我这里的输出吗