-1

i am trying to use d3.js tree layout and it uses the input json in particular format only. For example

{
"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"}
                ]
            }
        ]
    }

  ]
};

i am also referring this fiddle example Modify lines/tooltip on hover what i want to know is if it's possible to convert xml data to json in any format using javascript.

the xml which gets generated is as below:

<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>

this xml data gets created dynamically with 0 to many planets nodes as i have used jqtree and placed a plus button next to planets node name.

i want to create a json in below format

{
    "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"
                        }
                    ]
                }
            ]
        }
    ]
}
Community
  • 1
  • 1
sugar
  • 251
  • 4
  • 13
  • 1
    Yes... do you have a specific question? – Ben Lyall May 03 '15 at 10:36
  • yes i have, i will tell you in a little detail.i will edit my question – sugar May 03 '15 at 10:42
  • When you do, please fix the "i" with "I" too, as in English language, there is no word as "i". They really hurt my eyes... – ilter May 03 '15 at 11:04
  • I'm confused. Doesn't jqtree take JSON and convert it to HTML? In which case, can't you just use the JSON you create to represent the tree and manipulate that? – Ben Lyall May 03 '15 at 12:09
  • no I have a stand alone html form which on submit creates a xml file. now I need to represent this data in grapical format . Hence that is why i chose to use d3.js – sugar May 03 '15 at 12:36

1 Answers1

0

Yes you can convert XML to JSON, this is an article by David Walsh detailing exactly that http://davidwalsh.name/convert-xml-json.

Alex Baulch
  • 751
  • 1
  • 11
  • 24