Note that there are some explanatory texts on larger screens.

plurals
  1. POFind descendant from XML using jQuery
    text
    copied!<p>I have an XML file:</p> <pre><code> &lt;?xml version="1.0" encoding="ISO-8859-1"?&gt; &lt;childrens&gt; &lt;child id="1" value="Root Catalog" parent_id="0"&gt; &lt;child id="2" value="Apparel" parent_id="1"&gt; &lt;child id="3" value="Accessories" parent_id="2"&gt; &lt;child id="4" value="Handbags" parent_id="3"&gt; &lt;child id="5" value="Jewelry" parent_id="4"/&gt; &lt;child id="6" value="test1" parent_id="4"/&gt; &lt;child id="7" value="test2" parent_id="4"/&gt; &lt;child id="15" value="test3" parent_id="4"/&gt; &lt;/child&gt; &lt;/child&gt; &lt;/child&gt; &lt;child id="8" value="test_A" parent_id="1"&gt; &lt;child id="9" value="test_B" parent_id="8"&gt; &lt;child id="10" value="test_C" parent_id="9"&gt; &lt;child id="11" value="test_D" parent_id="10"/&gt; &lt;/child&gt; &lt;/child&gt; &lt;/child&gt; . . . . . . &lt;child id="1111" value="test" parent_id="1"&gt; &lt;child id="1112" value="test1" parent_id="1111"&gt; &lt;child id="1113" value="test12" parent_id="1112"&gt; &lt;child id="1114" value="test123" parent_id="1113"/&gt; &lt;child id="1115" value="test1234" parent_id="1114"/&gt; &lt;/child&gt; &lt;/child&gt; &lt;child id="1116" value="test12345" parent_id="1111"/&gt; &lt;/child&gt; &lt;/child&gt; &lt;/childrens&gt; </code></pre> <p>I want to find all the <em>descendants</em> (with all children up until leaf node) of a particular node. For example, here <code>test</code>'s descendants are <code>test1,test12,test123,test1234 &amp; test12345</code></p> <p>If I find descendants for <code>test1</code>, then result will be <code>test12,test123,test1234</code>.</p> <pre><code>$(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); }); } }); }); </code></pre> <p>The use of jQuery <code>.children()</code> gives me only that node's immediate child. It will not give its grandchildren. For example, for <code>test</code> it will alert only <code>test1 &amp; test12345</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload