2011-04-30 76 views
0

我想在解析XML文檔時生成HTML。問題是我有變量需要分析之前被分析 - 任何人都可以告訴我一個解決方法嗎?用JQuery解析XML(需要邏輯幫助)

$.ajax({ 
    url: 'doc.xml', 
    type:'get', 
    dataType:'xml', 
    async:'false', 
    success:function(xmlReply){ 
     var out = ''; 
     var businfo = ''; 
     var businfoOne = ''; 
     var latFrom = ''; 
     var longFrom = ''; 
     var locationAt = ''; 
     var latTo = ''; 
     var longTo = ''; 
     $("#result").empty(); 

     out += '<ul id=\'resultslist\' data-role=\'listview\' data-theme=\'c\'>'; 
     $(xmlReply).find('itinerarie').each(function(){ 

      out += '<li data-role=\'list-divider\' data-theme=\'a\'>'; 
      locationAt = $(this).find('description').text(); 
      out += locationAt; 
      out += '</li>'; 
      out += '<li><a href=\"#route_results_map\" onclick=\"plotMap(\''+businfo+'\','+latFrom+','+longFrom+','+latTo+','+longTo+')\">'; 
      var count = 0; 
      var transfer = $(this).find('route').length; 
      $(this).find('route').each(function(){ 

       //display transfer 
       if(count > 0){out+= '>>> Transfer To >>><br/>';} 

       //display get on bus information 
       if(count == 0){ 
        businfo = $(this).find('bus').text(); 
       } 
       out += $(this).find('bus').text(); 

       $(this).find('geton').each(function(){ 
        //where to board the bus 
        if(count == 0){ 
         latFrom = $(this).find('lat').text(); 
         longFrom = $(this).find('long').text(); 
        } 
        $(this).find('name').text(); 
        out += '<br/>Leaves: '; 
        out += $(this).find('time').text(); 
       }) 

        //end geton 
        $(this).find('getoff').each(function(){ 
         $(this).find('name').text(); 
         out += ' <br/>Reaches: '; 
         out += $(this).find('time').text(); 
         out += '<br/>'; 
         latTo = $(this).find('lat').text(); 
         longTo = $(this).find('long').text(); 
        }) 
         count++; 
      }) 
       out += '</a></li>'; 
     }) 
      //out += '</ul>'; 
      $("#result").append($(out)); 
     $("#result").append('<ul>'); 
     $('#resultslist').listview(); 
    }//success 
});//ajax 
+0

你想分配哪個變量在哪裏,你可以給更多的細節 – kobe 2011-04-30 18:36:05

+0

hi kobe,基本上我想分配的變量是我解析的變量 - 所以現在,最終的結果是:第1行有不是值,第2行的值爲第1行,第3行的值爲第2行。 – dtwy 2011-04-30 20:03:25

回答

2

在你上面的代碼,我看不出一個理由,爲什麼你不能耽誤你解析你的XML之後建立你的最終輸出。我稍微改了一下你的代碼,我添加了一個新的臨時變量parse_out,在這個變量中你可以在解析的時候爲這個項目創建HTML內容,然後在解析完所有內容之後,這就是當你將<li>添加到最終輸出時現在設置的變量)以及來自parse_out臨時變量的HTML內容。

$.ajax({ 
    url: 'doc.xml', 
    type:'get', 
    dataType:'xml', 
    async:'false', 
    success:function(xmlReply){ 
     var out = ''; 
     var businfo = ''; 
     var businfoOne = ''; 
     var latFrom = ''; 
     var longFrom = ''; 
     var locationAt = ''; 
     var latTo = ''; 
     var longTo = ''; 
     $("#result").empty(); 

     out += '<ul id=\'resultslist\' data-role=\'listview\' data-theme=\'c\'>'; 
     $(xmlReply).find('itinerarie').each(function(){ 

      locationAt = $(this).find('description').text(); 
      var count = 0; 
      var transfer = $(this).find('route').length; 
      var parse_out = ""; 
      $(this).find('route').each(function(){ 

       //display transfer 
       if(count > 0){parse_out+= '>>> Transfer To >>><br/>';} 

       //display get on bus information 
       if(count == 0){ 
        businfo = $(this).find('bus').text(); 
       } 
       parse_out += $(this).find('bus').text(); 

       $(this).find('geton').each(function(){ 
        //where to board the bus 
        if(count == 0){ 
         latFrom = $(this).find('lat').text(); 
         longFrom = $(this).find('long').text(); 
        } 
        $(this).find('name').text(); 
        parse_out += '<br/>Leaves: '; 
        parse_out += $(this).find('time').text(); 
       }); 

        //end geton 
        $(this).find('getoff').each(function(){ 
         $(this).find('name').text(); 
         parse_out += ' <br/>Reaches: '; 
         parse_out += $(this).find('time').text(); 
         parse_out += '<br/>'; 
         latTo = $(this).find('lat').text(); 
         longTo = $(this).find('long').text(); 
        }); 
         count++; 
      }); 
      out += '<li data-role=\'list-divider\' data-theme=\'a\'>'; 
      out += locationAt; 
      out += '</li>'; 
      out += '<li><a href=\"#route_results_map\" onclick=\"plotMap(\''+businfo+'\','+latFrom+','+longFrom+','+latTo+','+longTo+')\">'; 
      out += parse_out; 
      out += '</a></li>'; 
     }); 
     out += '</ul>'; 
     $("#result").append($(out)); 
     $('#resultslist').listview(); 
    }//success 
});//ajax 

我也沒明白你爲什麼註釋掉關閉您<ul>在你處理的到底爲什麼要附加您的輸出後,空<ul>,我改變了有點什麼,我想可能是什麼你自找的。