2017-09-02 59 views
0

這是一個Web服務的響應XML:解析XML對象HTML UI元素

<information> 
    <customer> 
     <customer_id>asdf1_id</customer_id> 
     <customer_name>asdf1</customer_name> 
    </customer> 
    <customer> 
     <customer_id>asdf2_id</customer_id> 
     <customer_name>asdf2</customer_name> 
    </customer> 
    <customer> 
     <customer_id>asdf3_id</customer_id> 
     <customer_name>asdf3</customer_name> 
    </customer> 
</information> 

而且,我需要獲取每一位客戶,它解析爲一個HTML <ul>。所以基本上,web服務返回這個XML與1,2,3,4,或任何數量的客戶,我需要的AJAX解析這個信息 到<ul> html元素,應該看起來像這樣:

<li onclick='UpdateCustomer(asdf1_id)'>asdf1</li> 
<li onclick='UpdateCustomer(asdf2_id)'>asdf2</li> 
<li onclick='UpdateCustomer(asdf3_id)'>asdf3</li> 

那麼,什麼是Ajax代碼,我需要解析<ul>中的XML響應<li>對象?

回答

0

試試這個:

的jQuery:

$(document).ready(function() { 
    $.ajax({ 
     type: "GET", 
     url: "information.xml", 
     dataType: "xml", 
     success: function(response) { 
      $('customer', response).each(function() { 
       $(".ulcontainer").append($("<li onclick=UpdateCustomer(" + $(this).find("customer_id").text().trim() + ")>" + $(this).find("customer_name").text() + "</li>")); 
      }); 
     } 
    }); 
}); 

HTML:

<ul class="ulcontainer"> 

</ul> 

基本上把你的XML到一個文件information.xml並使用GET請求中抓取數據。當數據成功獲取時,使用jQuery each函數遍歷每個customer標籤並獲取其中的信息。

編輯:我修改了我的AJAX調用,給你想要的<li>上面描述的標記。