www.webdeveloper.com
Results 1 to 2 of 2

Thread: Issue accessing multiple xml child nodes using JS

  1. #1
    Join Date
    Apr 2014
    Posts
    1

    Question Issue accessing multiple xml child nodes using JS

    Im working on an application where i need to fetch data and im using a js function to do the same

    My xml is this http://pastebin.com/sUd3Z7QK

    and the js function for fetching data is as follows

    Code:
    else if(requestType == relatedProductsRequest){       
    ArrayForRelatedProductsDetails =[];
    var related_product_ids=[];
    console.log("inside relatedProductsRequest::::::::");   
    
    for(i = 0 ; i < successData.length; i++) {           
        var r_product_id = successData[i].getElementsByTagName("Product_id")[0].childNodes[0].nodeValue;
        for(i=0;i<successData.length;i++)
        {          related_product_ids.push(successData[i].getElementsByTagName("Related_product_id")[i]);
                 }
        related_product_ids=related_product_ids.filter(function(n){
            return n != undefined
        });          
        for(i=0;i<related_product_ids.length;i++) {   
                       if(productSelected == r_product_id){                
                ArrayForRelatedProductsDetails.push(related_product_ids[i].innerHTML);   
                            }}
        fetchData(relatedProductInfoRequest);
        requestType = NoContentDisplayRequest;
    }
    }
    here Here sucessData is the array containing the entire xml (using xmlDoc.getElementsByTagName(root))

    http://i.stack.imgur.com/ARr9b.png

    the first for loop will loop till the length of the sucessdata array(i.e xml size) with the 2nd for loop im getting all the related product ids and storing them in an array

    http://i.stack.imgur.com/dwsUt.png

    and with the 3rd loop im iterating matching the selected product id with the product id's in the xml and on match im storing the related prodict ids and proceeding

    now my issue is this code doesnt proceed ahead of the first root node in the xml i.e doesnt go beyond the first product id 1001310101

    now previously when i was using this xml structure

    http://pastebin.com/qDNz28m4

    i had this js code which gave the proper output

    else if(requestType == relatedProductsRequest){
    //console.log("inside relatedProductsRequest::::::::");
    ArrayForRelatedProductsDetails =[];
    for(i = 0 ; i < successData.length; i++) {
    var r_product_id = successData[i].getElementsByTagName("Product_Id")[0].childNodes[0].nodeValue;
    var related_product_id = successData[i].getElementsByTagName("Related_product_id")[0].childNodes[0].nodeValue;

    if(productSelected == r_product_id){
    ArrayForRelatedProductsDetails.push(related_product_id);
    }
    }
    fetchData(relatedProductInfoRequest);
    requestType = NoContentDisplayRequest
    }
    so what am i doing wrong in my js? or is my xml structure wrong?
    i hope ive put my question across properly thanks in advance

  2. #2
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    typically white space between elements is the bane of DOM iteration.
    if you can strip out the white space "pre-flight" (e.g. before it goes to the client-side) that should help. you may have to check the nodeType of the
    Code:
    successData[i].getElementsByTagName("Product_Id")[0].childNodes[0]
    first.
    i haven't touched javascript based XML processing in 7 years. D:

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles