使用通用键而不是元素名称作为键将xml转换为json

而不是元素名称作为JSON中的键,我需要生成更多的通用键JSON,描述节点types。 d3.js使用这些types的json文件来生成可视化文件。 使用node.js模块xml2js,我parsing了xml文档的简化版本后得到这个:

{ "XML": { "$": { "xmlns": "http://example.org/example/2011/1" }, "Building": [ { "BuildingID": [ { "$": { "id": "BuildingAudit" } } ], "ProjectStatus": [ { "Date": [ "2013-07-16" ], "EventType": [ "audit" ] } ] }, { "BuildingID": [ { "$": { "id": "BuildingRetrofit" } } ], "ProjectStatus": [ { "Date": [ "2013-08-06" ], "EventType": [ "job completion testing/final inspection" ] } ] } ], "Project": [ { "BuildingID": [ { "$": { "id": "BuildingRetrofit" } } ], "ProjectDetails": [ { "ProjectStatus": [ { "Date": [ "2013-08-06" ], "EventType": [ "job completion testing/final inspection" ] } ], "ProjectSystemIdentifiers": [ { "$": { "id": "Project_JobCompletion" } } ] } ], "ProjectID": [ { "$": { "id": "Project" } } ] } ] } 

我需要的是更像这样的元素名称键是“名称”和孩子是可识别节点与“孩子”的关键:

 { "name": "flare", "children": [ { "name": "analytics", "children": [ { "name": "cluster", "children": [ {"name": "AgglomerativeCluster", "size": 3938}, {"name": "CommunityStructure", "size": 3812}, {"name": "HierarchicalCluster", "size": 6714}, {"name": "MergeEdge", "size": 743} ] }, { "name": "graph", "children": [ {"name": "BetweennessCentrality", "size": 3534}, {"name": "LinkDistance", "size": 5731}, {"name": "MaxFlowMinCut", "size": 7840}, {"name": "ShortestPaths", "size": 5914}, {"name": "SpanningTree", "size": 3416} ] } ] } ] } 

xml文档有很多types的元素(比上面的例子多),并且嵌套得相当深。 此外,我不知道什么元素将呈现。 许多这些元素是可选的。 我打开任何types的解决scheme(JavaScript) – 无论是在XMLparsing器上设置选项或有条件地复制deepClonetypes的操作上的数据。

谢谢