M.I.T.
M.I.T.

Reputation: 1042

Find descendant from XML using jQuery

I have an XML file:

    <?xml version="1.0" encoding="ISO-8859-1"?>
<childrens>
 <child id="1" value="Root Catalog" parent_id="0">
  <child id="2" value="Apparel" parent_id="1">
    <child id="3" value="Accessories" parent_id="2">
        <child id="4" value="Handbags" parent_id="3">
            <child id="5" value="Jewelry" parent_id="4"/>
            <child id="6" value="test1" parent_id="4"/>
            <child id="7" value="test2" parent_id="4"/>
            <child id="15" value="test3" parent_id="4"/>
        </child>
    </child>
  </child>
  <child id="8" value="test_A" parent_id="1">
    <child id="9" value="test_B" parent_id="8">
        <child id="10" value="test_C" parent_id="9">
            <child id="11" value="test_D" parent_id="10"/>
        </child>
    </child>
  </child>
  .
  .
  .
  .
  .
  .
  <child id="1111" value="test" parent_id="1">
    <child id="1112" value="test1" parent_id="1111">
        <child id="1113" value="test12" parent_id="1112">
            <child id="1114" value="test123" parent_id="1113"/>
            <child id="1115" value="test1234" parent_id="1114"/>
        </child>
    </child>
    <child id="1116" value="test12345" parent_id="1111"/>
  </child>  
 </child>
</childrens>

I want to find all the descendants (with all children up until leaf node) of a particular node. For example, here test's descendants are test1,test12,test123,test1234 & test12345

If I find descendants for test1, then result will be test12,test123,test1234.

$(document).ready(function(){
$.ajax({
    type: "GET",
    url: "test.xml",
    dataType: "xml",
    success: function(xml) {
        $(xml).find('child[value="test"]').children().each(function(){
            var i = $(this).attr('value');
            alert(i);

        });
    }
});
});

The use of jQuery .children() gives me only that node's immediate child. It will not give its grandchildren. For example, for test it will alert only test1 & test12345.

Upvotes: 4

Views: 1308

Answers (2)

codetantra
codetantra

Reputation: 258

In a more layman and primitive way you could do like this :

    function iterative_logger(root, t) {
        if (t > 0) console.log(root.attr('value'));
        //console.log("Traversing : " + root.attr('value')+"\n");
        var root_children = $(root).children();
        //console.log("Found " + root_children.length + " children\n");
        if (root_children.length > 0)
            root_children.each(function () {
                t++;
                //console.log("\t" + $(this).attr('value'));
                iterative_logger($(this), t);
            });
    }
    $(document).ready(function () {
        $.ajax({
            type: "GET",
            url: "test.xml",
            dataType: "xml",
            success: function (xml) {
                iterative_logger($(xml).find('child[value="test"]'), 0);
            }
        });
    });

This recursively computes all the descendant nodes of a given node 'root'.

Upvotes: 0

You can achieve it by doing a pre-order traversal. Its a recursive function witch will process the nodes the order you want. See How to write a simple preorder DOM tree traversal algorithm in jQuery?

Considering @k-prime answer, your example will be:

$(xml).find('child[value="test"]').children().each (function processNodes()
{
    alert($(this).attr('value'));
    if (this.nodeType != 3)
        $(this).children().each(processNodes);
});

JsFiddle

As a separated function:

function recursiveDescendantsValues(node, arr) {
    node.children().each(function () {
        arr.push($(this).attr('value'));
        if ($(this).nodeType != 3) {
            recursiveDescendantsValues($(this), arr);
        }
    });
}
jQuery.fn.descendantsValues = function() {
    var arr = []
    recursiveDescendantsValues($(this), arr);
    return arr;
};

JsFiddle

Hope it helps!

Upvotes: 1

Related Questions