2

我需要使用更通用的键生成 json,而不是元素名称作为 json 中的键,以描述节点类型。d3.js 使用这些类型的 json 文件来生成可视化。使用 node.js 模块 xml2js,我在解析 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 文档有许多类型的元素(比上面的示例更多)并且嵌套得相当深。此外,我不知道先验元素会出现什么。许多这些元素是可选的。我对任何类型的解决方案(javascript)持开放态度——无论是在 xml 解析器上设置选项还是在 deepClone 类型的操作上有条件地复制数据。

谢谢

4

0 回答 0