我有一个中等大小的xml~5mb需要转换为csv。
显然不会重新发明轮子, 所以两层方法 - 1 GT; 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 ..
The Json I get on converting xml
另外根据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,如下所示:Example Perfect CSV From JSON
编辑1: 我正在寻找的格式有点像, 捕获每个人员节点的所有父组织和organizationalUnit详细信息非常重要。 例如,
organizationalUnit UUID“b3b05b77-a8a7-43ed-ab74-b7d898c60296”应该 生成CSV行,如:
"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"
编辑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会在错误的列中获取一些数据行:
Wrong csv after camaro 有关如何使用我的输入工作的任何提示。
答案 0 :(得分:0)
因为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)