Note that there are some explanatory texts on larger screens.

plurals
  1. POTraverse xml recursively with JQuery
    primarykey
    data
    text
    <pre><code> &lt;div id="page-wrap"&gt;&lt;/div&gt; $(document).ready(function(){ var xml = "&lt;root&gt; \ &lt;method name='A'&gt; \ &lt;childcall name='B'&gt;&lt;/childcall&gt; \ &lt;childcall name='C'&gt;&lt;/childcall&gt; \ &lt;/method&gt; \ &lt;method name='B'&gt; \ &lt;childcall name='D'&gt;&lt;/childcall&gt; \ &lt;/method&gt; \ &lt;method name='C'&gt; \ &lt;childcall name='D'&gt;&lt;/childcall&gt; \ &lt;childcall name='E'&gt;&lt;/childcall&gt; \ &lt;/method&gt; \ &lt;method name='D'&gt; \ &lt;childcall name='F'&gt;&lt;/childcall&gt; \ &lt;/method&gt; \ &lt;/root&gt;"; var data = $.parseXML(xml); console.log(data); //alert(data); var htmltxt="&lt;ul&gt;"; $(data).find('method').each(traverseXml); htmltxt = htmltxt + "&lt;/ul&gt;"; //alert(htmltxt); $("#page-wrap").html(htmltxt); function traverseXml(data){ var namenode = $(this).attr('name'); var count = 0; $(this).children('childcall').each(function(){ count++; }); if(count&gt;0){ htmltxt = htmltxt + "&lt;li class='category'&gt;" + namenode +"&lt;ul&gt;"; $(this).children('childcall').each(function(){ var name = $(this).attr('name'); htmltxt = htmltxt + "&lt;li&gt;" + name + "&lt;/li&gt;"; }); htmltxt = htmltxt + "&lt;/ul&gt;&lt;/li&gt;"; }else{ htmltxt = htmltxt +"&lt;li&gt;"+namenode+"&lt;/li&gt;"; } } $('li.category').addClass('plusimageapply'); $('li.category').children().addClass('selectedimage'); $('li.category').children().hide(); $('li.category').each( function(column) { $(this).click(function(event){ if (this == event.target) { if($(this).is('.plusimageapply')) { $(this).children().show(); $(this).removeClass('plusimageapply'); $(this).addClass('minusimageapply'); } else { $(this).children().hide(); $(this).removeClass('minusimageapply'); $(this).addClass('plusimageapply'); } } }); } ); }); </code></pre> <p>The code traverses the one level of xml. Here is the fiddle - <a href="http://jsfiddle.net/CKa6V/10/" rel="nofollow">http://jsfiddle.net/CKa6V/10/</a></p> <p>I am looking to traverse the xml nodes recursively such that every node expands to the last child. For e.g. A expands to B, B then becomes expandable and expands to D and finally D expands to F. Similarly for all nodes.</p> <p>EDIT: Sorry if the question was not clear. I am looking for the following output-</p> <pre><code>A |__B |__D |__F |__C |__D |__F |__E B |__D |__F C |__D |__F |__E D |__F </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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