JsTree with custom xml or json structure

318 views Asked by At

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

1

There are 1 answers

0
Stephen S On

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.

function getFeature(s, t) {
    t.text = s["Name"] ? s["Name"] : "";
    if (s.hasOwnProperty("Subfeatures")) {
        t.children = []; 
        s.Subfeatures.Feature.forEach(function (f, i) {
            t.children.push({})
            getFeature(f, t.children[i]);
        });
    }
}

function ParseData() {
    //Convert XML to JSON prior to this.

    var oSourceJSON = {
        "RootFeature": {
            "Name": "Feature1",
         ...
         ...
         ...

    };

    var result = [];
    if (oSourceJSON.hasOwnProperty("RootFeature")) {
        result.push({});
        getFeature(oSourceJSON.RootFeature, result[0]);
    }
    return result;
}

$(document).ready(function () {
    $('#jstree_demo_div').jstree({
        core: {
            //Note: If you use an external service use a callback method here
            data: ParseData()
        }
    });
});