The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Issue accessing multiple xml child nodes using JS, Issue accessing multiple xml child nodes using JS
vivekh
post Apr 14 2014, 02:03 AM
Post #1





Group: Members
Posts: 1
Joined: 14-April 14
Member No.: 20,719



Im working on an application where i need to fetch data from an xml with multiple child nodes 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 sucessData is the array containing the entire xml (using xmlDoc.getElementsByTagName(root))
i.stack.imgur.com/ARr9b.png

QUOTE
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
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 doesn't 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


CODE
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 new xml structure wrong?
i hope ive put my question across properly thanks in advance
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Frederiek
post Apr 14 2014, 03:07 AM
Post #2


Programming Fanatic
********

Group: Members
Posts: 5,146
Joined: 23-August 06
From: Europe
Member No.: 9



Have a look here on how such a thing can be done: http://www.javascriptkit.com/dhtmltutors/ajaxgetpost3.shtml
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Apr 14 2014, 06:31 AM
Post #3


.
********

Group: WDG Moderators
Posts: 9,656
Joined: 10-August 06
Member No.: 7



CODE
    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++) {

Above you nest two identical loops which may not be intended (if nothing else the variable i is reset).

Then further down there's a third nested loop using the same var i:

CODE
for(i=0;i<related_product_ids.length;i++)  

--give it another name like j instead.

Also check your browsers' error consoles for script errors.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 25th April 2024 - 12:22 PM