2

Instead of element names as keys in the json, I need to generate json with more generic keys, describing the node type. d3.js uses these types of json files to generate visualizations. Using the node.js module xml2js, I get this after parsing a simplified version of an 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"
                    }
                }
            ]
        }
    ]
}

What I need is something more like this, where the element name key is "name" and the children are identifiable nodes with a key of "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}
            ]
        }
        ]
    }
    ]
}

The xml documents have many types of elements (more than the example above) and are nested fairly deep. Additionally, I don't know a priori what the elements will be present. Lots of these elements are optional. I'm open to any type of solution (javascript) - whether that is setting options on an xml parser or conditionally copying data on a deepClone-type of operation.

Thank you

Jeff
  • 939
  • 10
  • 19
  • It would be really nice if d3.js had a way to convert generic XML files into the structure which it consumes for this type of chart. I haven't tried this solution yet, but I just saw this posted elsewhere: http://stackoverflow.com/a/19317823/1884101 – Jeff Oct 11 '13 at 16:56

0 Answers0