2012-09-04 96 views
1

我有我需要找到使用jQuery和Ajax 到目前爲止,我結識的對象,但我有兩個問題具體PK的項目XML文件中的XML元素值:查找使用jQuery

  1. 有沒有更好的主意,而不是做一個循環找到PK值?
  2. XML很大,我需要知道是否有更好的方法來查詢它,而不是將其加載到內存中,XSLT是否更好?或者有沒有辦法比jquery做得更好?

這裏是我的代碼

$.ajax({ 
     url: 'xml/products.xml', 
     dataType: 'html', 
     success: function(xml) { 
      $(xml).find('pk').each(function() 
      { 
       if($(this).text()=="1") 
        //do something 
      } 
}); 

這裏是我的XML

<products> 
<item> 
    <pk>1</pk> 
    <name>test</name> 
</item> 
<item> 
    <pk>2</pk> 
    <name>test2</name> 
</item> 
<item> 
    <pk>3</pk> 
    <name>test3</name> 
</item> 
<item> 
    <pk>4</pk> 
    <name>test4</name> 
</item> 
</products> 

回答

3

首先,您必須編寫正確的XML字符串,就像您必須完成/結束最後一個啓動的相同標記一樣。在上面的示例代碼中,您關閉時發生錯誤。它是錯誤的XML語法。請如下修正: 測試

這裏我有樣箱製造用於解析XML數據或標籤,而不是阿賈克斯我剛纔分析,因爲在垃圾箱上的按鈕單擊事件的XML數據的Ajax調用不是可能調用外部文件。

這裏是演示:http://codebins.com/bin/4ldqp7u

HTML

<div> 
    <input type="button" id="btnxml" value="Get XML Data" /> 
    <input type="button" id="btnreset" value="Reset" style="display:inline"/> 
    <div id="result"> 
    </div> 
</div> 
<div id="xmldata"> 
    <products> 
    <item> 
     <pk> 
     1 
     </pk> 
     <name> 
     test 
     </name> 
    </item> 
    <item> 
     <pk> 
     2 
     </pk> 
     <name> 
     test2 
     </name> 
    </item> 
    <item> 
     <pk> 
     3 
     </pk> 
     <name> 
     test3 
     </name> 
    </item> 
    <item> 
     <pk> 
     4 
     </pk> 
     <name> 
     test4 
     </name> 
    </item> 
    </products> 
</div> 

JQuery的:

$(function() { 
    $("#btnxml").click(function() { 
     var xml = "<rss version='2.0'>"; 
     xml += $("#xmldata").html(); 
     xml += "</rss>"; 
     var xmlDoc = $.parseXML(xml), 
      $xml = $(xmlDoc); 

     var result = ""; 
     if ($xml.find("item").length > 0) { 

      result = "<table class='items'>"; 
      result += "<tr><th>PK</th><th>Name</th></tr>"; 

      $xml.find("item").each(function() { 
       result += "<tr>"; 
       result += "<td>" + $(this).find("pk").text() + "</td>"; 
       result += "<td>" + $(this).find("name").text() + "</td>"; 
       result += "</tr>"; 
      }); 

      result += "</table>"; 
      $("#result").html(result); 
     } 


    }); 

    //Reset Result 
    $("#btnreset").click(function() { 
     $("#result").html(""); 
    }); 

}); 

CSS:

#xmldata{ 
    display:none; 
} 
table.items{ 
    margin-top:5px; 
    border:1px solid #6655a8; 
    background:#55a5d9; 
    width:20%; 
} 
table.items th{ 
    border-bottom:1px solid #6655a8; 
} 
table.items td{ 
    text-align:center; 
} 
input[type=button]{ 
    border:1px solid #a588d9; 
    background:#b788d9; 
} 

演示:http://codebins.com/bin/4ldqp7u

+0

我不知道爲什麼我覺得這個答案是好的,但遠遠沒有我想要的東西....我有外部的XML文件,所以我需要在Ajax調用點擊...和我的問題是如何通過ID – Lamis

+0

得到一個特定的節點嗨,我有修改箱和需要的變化,以找到Pk項從特定節點ID從XML數據。請點擊下面的鏈接並檢查它對你有用..? http://codebins.com/bin/4ldqp7u/2 – gaurang171

+0

足夠接近,謝謝:) – Lamis

1

最起碼,你可以使用更具體的查詢不僅僅是 「PK」。在這個例子中,$(xml).find("products item pk")應該更快。