I have an XML structure that look like this:
<RootFeature>
<Name>MainFeature</Name>
<Subfeatures>
<Feature>
<Name>Feature1</Name>
<Subfeatures>
<Feature>
<Name>Feature1_1</Name>
</Feature>
<Feature>
<Name>Feature1_2</Name>
</Feature>
<Feature>
<Name>Feature1_3</Name>
</Feature>
</Subfeatures>
</Feature>
<Feature>
<Name>Feature2</Name>
</Feature>
<Feature>
<Name>Feature3</Name>
</Feature>
<Feature>
<Name>Feature4</Name>
</Feature>
<Feature>
<Name>Feature5</Name>
</Feature>
<Feature>
<Name>Feature6</Name>
</Feature>
<Feature>
<Name>Feature7</Name>
</Feature>
</Subfeatures>
I would like to use Jstree on it, and create a tree. I've tried to apply Jstree directly on it (XML), and I have the same result as explained on this topic:
Populating jstree from xml string
After converting it to Json (using the following library:https://goessner.net/download/prj/jsonxml/), I have this:
"RootFeature":{
"Name":"Feature1",
"Subfeatures":{"Feature":[
{
"Name":"Feature1",
"Subfeatures":{"Feature":[
{"Name":"Feature1_1"},
{"Name":"Feature1_2"},
{"Name":"Feature1_3"}
]}
},
{"Name":"Feature2"},
{"Name":"Feature3"},
{"Name":"Feature4"},
{"Name":"Feature5"},
{"Name":"Feature6"},
{"Name":"Feature7"}
]}
I searched how to apply Jstree on my structure knowing that I have to follow a certain format where <Name>
tags have to be replaced by <text>
, <Subfeatures>
by <children>
, and get rid of the <Feature>
tags.
I found this topic on the forum:JsTree with custom json data but couldn't apply the solution..and did not understand it totally to be honest.
I even tried to recreate the tree recursively using the Json data, but I'm not able to create an array "Children" with multiple elements with same name "text", having each one a value (like a Json file finally). It's either:
"Children":
"text1":"Feature1",
"text2":"Feature2",
"text3":"Feature3",
or
"Children":
"0":"Feature1",
"1":"Feature2",
"2":"Feature3",
If anyone knows how could I deal with Jstree using my structure, or have any idea, you're welcome.
Thanks
jsTree expects data in a specific format to create the tree. Your structure needs to be parsed to a JSON structure similar to the one described here. Once you convert the XML to JSON, you can use a parser as below over it.