Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6892071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:29:20+00:00 2026-05-27T06:29:20+00:00

Using Jquery I am parsing an XML file and adding an attribute to every

  • 0

Using Jquery I am parsing an XML file and adding an attribute to every element nodes.
After manipulated xml is returned to the caller I can’t get the added attibute, it says it is undefined.

function loadTreeML(dname){

    var xmlDoc;
        $.ajaxSetup({
          async: false
        });
        $.ajax({
            type: "GET",
            url: dname,
            dataType: "xml",

            success: function(xml) {

             var firstBranch =$(xml).find('branch').get(0);

             if (typeof firstBranch != "undefined")
                traverse(firstBranch, {"children":1, "siblings":0, "level":-1, "depth":0, "weight":1, "leaves":0, "strahlernum":0});
             xmlDoc = xml;


            }

        });

//should return manipulated xml  
            return  xmlDoc;
        }

//updating attribute in traverse function
    $(tree).attr('stats',child_stats);

Attribute I am adding is JSON data in format

{"children":1, "siblings":0, "level":-1, "depth":0, "weight":1, "leaves":0, "strahlernum":0}



function traverse(tree, parent_stats) 
{
    var child_stats = {"children":0, "siblings":0, "level":0, "depth":0, "weight":1, "leaves":0, "strahlernum":0};  
    //Counting the child node

    $(tree).children().each(function(){

            var kid = $(this);
            if((kid.get(0).tagName.toLowerCase()== "branch")|| (kid.get(0).tagName.toLowerCase() == "leaf"))
            child_stats.children++;

        });

     //alert(child_stats.children); 
    //Sibling
     child_stats.siblings = parent_stats.children - 1;
     //alert(child_stats.siblings );
    //Level
     child_stats.level = parent_stats.level + 1;

     //DFS
    if($(tree).get(0).tagName.toLowerCase() == "branch")
    {
        var offset = -1;
        $(tree).children().each(function(){

            var kid = $(this);
            if((kid.get(0).tagName.toLowerCase()== "branch")|| (kid.get(0).tagName.toLowerCase() == "leaf")){
            var temp_stats = traverse(kid,child_stats);
                //count leaves
                child_stats.leaves += temp_stats.leaves;
                //determine depth
                if(child_stats.depth < temp_stats.depth+1) child_stats.depth = temp_stats.depth+1;
                //compute weight
                child_stats.weight += temp_stats.weight;
                //computer strahler
                if ((child_stats.strahlernum != temp_stats.strahlernum)&&(child_stats.strahlernum != 0)) offset = -2;
                child_stats.strahlernum = Math.max(child_stats.strahlernum, temp_stats.strahlernum);

            }
            child_stats.strahlernum += child_stats.children + offset;

        });

    } 
    else { //leaf
       child_stats.leaves = child_stats.strahlernum = 1;
    }

    //Adding stats attribute write to XML
    $(tree).attr('stats',child_stats);

    return child_stats;
}

I am not getting the stats attribute in the returned xmlDoc that i have added. It is saying undefined. Let me know what I am doing wrong here.
Thank you very much for your time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:29:20+00:00Added an answer on May 27, 2026 at 6:29 am

    Fixed the stats property problem.
    Accoring to JQuery
    .attr() should not be used on plain objects, arrays, the window,

    You have to use .data(‘stats’, {}) to set object base data like json. I have added the code in stats property is showing properly now

    $(tree).data('stats',child_stats);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am parsing an xml file using jQuery and need to be able to
I'm having difficulty parsing some JSON data returned from my server using jQuery.ajax() To
Using jQuery , how can I dynamically set the size attribute of a select
I am using jQuery.ajax to parse some xml from a file. Everything works fine
I'm trying to fumble my way through parsing rss sensibly, using jQuery and jFeed
Using jquery how do I focus the first element (edit field, text area, dropdown
Using jQuery, what's the best way to find the next form element on the
I'm retreiving a JSON string and parsing it with jQuery with $.getJSON. After I
I'm trying to extract values from xml using jQuery in a cross-browser compatible fashion.
I love jQuery. I am probably going to have some XML parsing and manipulation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.