可以将xml转换为任何json格式

时间:2015-05-03 10:29:24

标签: xml json d3.js

我正在尝试使用d3.js树布局,它只使用特定格式的输入json。例如

{
"name": "Base",
"children": [
    {
        "name": "Type A",
        "children": [
            {
                "name": "Section 1",
                "children": [
                    {"name": "Child 1"},
                    {"name": "Child 2"},
                    {"name": "Child 3"}
                ]
            },
            {
                "name": "Section 2",
                "children": [
                    {"name": "Child 1"},
                    {"name": "Child 2"},
                    {"name": "Child 3"}
                ]
            }
        ]
    }

  ]
};

我也在提到这个小提琴示例Modify lines/tooltip on hover  我想知道的是,是否可以使用javascript以任何格式将xml数据转换为json。

生成的xml如下:

<universe>
 <universename>unique</universename>
 <creator>god</creator>
 <planets>
   <planetname>earth</planetname>
   <position>third</position>
   <countries>
     <countryname>country-1</countryname>
     <countrypopulation>xxx</countrypopulation>
   </countries>
   <waterbodies>
     <ocean>
       <oceanname>pacific</oceanname>
     </ocean>
   </waterbodies>
   <mountains>
    <mountainname>everest</mountainname>
   </mountais>
  </planets>

 <planets>
   <planetname>jupiter</planetname>
   <position>fourth</position>
   <countries>
     <countryname>country-2</countryname>
     <countrypopulation>xxx</countrypopulation>
   </countries>
   <waterbodies>
     <ocean>
       <oceanname>pacific-1</oceanname>
     </ocean>
   </waterbodies>
   <mountains>
    <mountainname>everest-2</mountainname>
   </mountais>
  </planets>  

</universe>

这个xml数据是用0到多个行星节点动态创建的,因为我使用了jqtree并在行星节点名称旁边放了一个加号按钮。

我想以下面的格式创建一个json

{
    "name": "universe",
    "value_list": [
        {
            "name": "universename",
            "value": "unique"
        },
        {
            "name": "creator",
            "value": "god"
        }
    ],
    "children": [
        {
            "name": "planets",
            "value_list": [
                {
                    "name": "planetname",
                    "value": "earth"
                },
                {
                    "name": "position",
                    "value": "third"
                }
            ],
            "children": [
                {
                    "name": "countries",
                    "value_list": [
                        {
                            "name": "countryname",
                            "value": "country-1"
                        },
                        {
                            "name": "countrypopulation",
                            "value": "xxx"
                        }
                    ]
                },
                {
                    "name": "waterbodies",
                    "children": [
                        {
                            "name": "ocean",
                            "value_list": [
                                {
                                    "name": "oceanname",
                                    "value": "pacific"
                                }
                            ]
                        }
                    ]
                },
                {
                    "name": "mountains",
                    "value_list": [
                        {
                            "name": "mountainname",
                            "value": "everest"
                        }
                    ]
                }
            ]
        },
        {
            "name": "planets",
            "value_list": [
                {
                    "name": "planetname",
                    "value": "jupiter"
                },
                {
                    "name": "position",
                    "value": "fourth"
                }
            ],
            "children": [
                {
                    "name": "countries",
                    "value_list": [
                        {
                            "name": "countryname",
                            "value": "country-2"
                        },
                        {
                            "name": "countrypopulation",
                            "value": "xxx"
                        }
                    ]
                },
                {
                    "name": "waterbodies",
                    "children": [
                        {
                            "name": "ocean",
                            "value_list": [
                                {
                                    "name": "oceanname",
                                    "value": "pacific-1"
                                }
                            ]
                        }
                    ]
                },
                {
                    "name": "mountains",
                    "value_list": [
                        {
                            "name": "mountainname",
                            "value": "everest-2"
                        }
                    ]
                }
            ]
        }
    ]
}

0 个答案:

没有答案