Not showing your js function doesn't help much. All .group are descendants of .map, it's not about context.
Maybe you want this (rough): $('.map').each(function(){ var xml = '<map>'; $(this).find('> .group').each(function(){ xml += '<group>'; $(this).find('> .group').each(function(){ xml += '<group>'; xml += $(this).text(); xml += '</group>'; }); xml += '</group>'; }); xml+= '</map>'; }); On Mar 3, 5:27 pm, DReed <danreed...@gmail.com> wrote: > I have been struggling with trying to do the following... perhaps I am > looking at it "too" hard since I have a feeling the solution is right > in front of me (i hope). > > I have something like the following: > > <div class="map"> > <div class="title">Title</div> > <div class="group"> > Group Name 1 > <div class="group">Group Name 1-1</div> > </div> > <div class="group"> > Group Name 2 > <div class="group">Group Name 2-1</div> > </div> > </div> > > I would like to turn this into XML, like: > > <map> > <title>Title</title> > <group>Group Name 1 > <group>Group Name 1-1</group> > </group> > <group>Group Name 2 > <group>Group Name 2-1</group> > </group> > </map> > > I have been trying using the .each() function but it doesn't seem to > hold the context node (when my context is the .map node and I want to > iterate the .group, it will iterate ALL of them, instead of the TWO > directly under the .map element. I can brute force this with some > recursion but I thought there must be a way to retain the context of > the jQuery. > > Perhaps the .each way isn't the right approach? > > Thanks in advance.