0

i have a XML file which is

<?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>
</childrens>

now i want to find the leaf element of a particular elements

for example i want to find leaf element of Apparel

which is Jewelry, test1, test2, test3 so i want this as my output

i have written this jQuery code to find leaf element

$.ajax({
type: "GET",
url: "test.xml",
dataType: "xml",
success: function(xml) {
    $(xml).find('child:empty').filter('child[value="Apparel"]').each(function(){
        var id = $(this).attr('id');
        alert(id)
    });
}
});

if i use only child:empty then it gives me all the leaf element

mine code is not working ...

1 Answer 1

1

see the question Traversing to the last child element in a tree using jQuery selector

You can do it this way:

$(text).find('child[value="Apparel"]').find(':not(:has(*))').each(function(i, item) {
    console.log($(item).attr('id') + " : " + $(item).attr('value'));
});
Sign up to request clarification or add additional context in comments.

1 Comment

Please Copy The XML file then you can figure out test_D is not leaf node of Apparel. you can also understand by id and parent_id. test_D is leaf node of test_A

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.