/* * @param dom_obj xml data * @param ulObj unordered list element */ function buildTree(dom_obj, ulObj){ ulObj.append('<li>'+dom_obj[0].nodeName+'</li>'); if(dom_obj[0].hasChildNodes()){ ulObj.append('<li><ul></ul></li/>'); dom_obj.children().each(function (i, obj){ buildTree($(this), ulObj.find('ul:last')); }) } }
buildTree($(xml), $('#demo ul')) <div id="demo"> <ul></ul> </div> ================== XML <RecentTutorials> <Tutorial author="The Reddest"> <Title>Silverlight and the Netflix API</Title> <Categories> <Category>Tutorials</Category> <Category>Silverlight 2.0</Category> </Categories> <Date>1/13/2009</Date> </Tutorial> </RecentTutorials> RESULT <ul> <li>#document</li> <li> <ul> <li>RecentTutorials</li> <li> <ul> <li>Tutorial</li> <li> <ul> <li>Title</li> <li>Categories</li> <li> <ul> <li>Category</li> <li>Category</li> <li>Date</li> </ul> </li> </ul> </li> </ul> </li> </ul> </li> </ul> Hi, The problem is that "Date" have to be one level up. In general when I have 2 elements in a same level, and the second have children (which don't have children) and after I go through them the script didn't return in its last level, and is continue from the same level. How to make the script to go one level up?