0
<script>         
$(document).ready(function(){
            var xml = "<root> \
                        <method name='A'> \
                        <childcall name='B'></childcall> \
                        <childcall name='C'></childcall> \
                        </method> \
                        <method name='B'> \
                        <childcall name='D'></childcall> \
                        </method> \
                        <method name='C'> \
                        <childcall name='D'></childcall> \
                        <childcall name='E'></childcall> \
                        </method> \
                        </root>";

            var data = $.parseXML(xml);
            console.log(data);
            $(data).find('method').each(function(){
                var name = $(this).attr('name');
                $('<div class="items"></div>').html('<a href="'+name+'">'+name+'</a>').appendTo('#page-wrap');

            });
        });

     </script>
</head>
<body>
    <div id="page-wrap"></div>
</body>
</html>

This code outputs A B C for parent method tag. The required output is A B C B D C D E. How do I traverse the child nodes recursively to get the required output? Would that be a depth-first-search?

2 Answers 2

1

Try this:

// Loop through the parent items
$(data).find('method').each(function () {
    var name = $(this).attr('name');
    $('<div class="items"></div>').html('<a href="' + name + '">' + name + '</a>').appendTo('#page-wrap');

    // Loop through the child items
    $(this).find('childcall').each(function () {
        name = $(this).attr('name');
        $('<div class="items"></div>').html('<a href="' + name + '">' + name + '</a>').appendTo('#page-wrap');
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0

if someone interested building all nested nodes , the updated code is below

$(xml).find('thymeSiteMap').each(function () {
    var name = $(this).attr('en');
    var id= $(this).attr('id');
    console.log(id);
    $('<div class="items shift" id="'+id+'"></div>').html('<a href="' + name + '">' + name + '</a>').appendTo('#page-wrap');

    // Loop through the child items
    $(this).find('thymeNode').each(function () {
    	
    	var parent = $(this).parent().attr('id');
    	console.log("me "+$(this).attr('id') +" my parent "+parent);
        name = $(this).attr('en');
        var divname="div#"+parent+".items";
        console.log(divname);
        var id= $(this).attr('id');
    
        $('<div class="items shift" id="'+id+'"></div>').html('<a href="' + name + '">' + name + '</a>').appendTo(divname);
    });
<!doctype html>
    <html >
      <head>
        <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
       
       <style>
       
       .shift{
       	
       	position:relative;
       	left:20px;
       }
       
       
       </style>
      </head>
      <body>
        <div id="page-wrap" class="shift"></div>
 
      </body>
    </html>

Comments

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.