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

时间:2013-08-25 02:15:53

标签: javascript json node.js xml-parsing d3.js

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

{
"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”,子元素是具有“children”键的可识别节点:

{
"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类型的操作上有条件地复制数据。

谢谢

0 个答案:

没有答案